Multiple choice technology security

Are there any memory issues in the following code? Please assume that variable inputsize has the correct size.

int add_num_array(int inputsize, int num) {
    int *newnum = malloc (inputsize * sizeof(int));  /* 1 */
    int i;   
    for (i=0; i

  1. No vulnerabilities are present

  2. Line 1 should only use malloc(inputsize);

  3. Line 2 should be for (i=0; i<=n, i++)

  4. Line 1 should use calloc() instead of malloc()

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The code uses malloc() which allocates uninitialized memory. At line 3, newnum[i] += num reads and adds to uninitialized garbage values, producing unpredictable results. calloc() should be used instead because it zero-initializes memory, ensuring clean arithmetic. Also note the code has a typo - n is undefined (should be inputsize).

AI explanation

The loop adds num into newnum[i] using +=, which reads the existing value at newnum[i] before adding — but malloc does not zero-initialize memory, so newnum[i] starts out as garbage. Using calloc() instead of malloc() would zero-initialize the allocated array, ensuring each += starts from a known value of 0 rather than uninitialized memory.