ABV.NON_ARRAY

Non-array object is used as an array

The ABV.NON_ARRAY checker finds defects when any non-array object is used as an array.

Vulnerability and risk

If non-array object is used as an array, then it might corrupt or misinterpret adjacent memory locations.

Mitigation and prevention

The compliant solution is to use only array-object which can be further used to access elements of array.

Vulnerable code example

Copy
extern int anything();

void foo(int *arr, int *num_pwr)
{
    *num_pwr = anything();
    arr[*num_pwr] = 5;
}

void bar(int *arr)
{
    int num_pwr;
    foo(arr, &num_pwr);
}
void blah()
{
    int singleton = 0;
    int *p = &singleton;
    bar(p);   // ABV.NON_ARRAY defect
}

Klocwork reports ABV.NON_ARRAY at line 18 because the pointer p, which holds the address of a non-array object, is passed to the function 'bar'. Within 'bar' (at line 6), this pointer is treated as an array, which can potentially corrupt or misinterpret adjacent memory locations.

Fixed code example

Copy
extern int anything();

void foo(int *arr, int *num_pwr)
{
    *num_pwr = anything();
    arr[*num_pwr] = 5;
}

void bar(int *arr)
{
    int num_pwr;
    foo(arr, &num_pwr);
}
void blah()
{
    int arr[2]= {0};
    int *p = arr;
    bar(p);
}

ABV.NON_ARRAY is not applicable in this case because p holds the base address of the array arr, not a non-array object. Therefore, no ABV.NON_ARRAY issue will occur at line 18 in this scenario.