[previous] 2950 [next] Constant: Negative value used in array subscript or pointer arithmetic operation.
Arrays

An integral constant expression with negative value is being added or subtracted from a pointer. Dataflow has been unable to determine the dimensions of the object which the pointer may be addressing. Is this intended ? If the original pointer and the resulting pointer do not address elements of the same array or the element one past the end of the array, the result will be invalid.

Notice that this message is not generated when subtracting a positive value from a pointer. It is only generated when adding or subtracting a potentially negative value.

For example:


/*PRQA S 488,489,492,510,2017,2211,2824,2982,2983,3132,3198,3199,3203,3227,3408,3447,3673,3680,3681 ++*/

extern void foo(int *p)
{
    int *pi;
    int  x;

    pi = p + 1;                         /*      */
    pi = p + (-1);                      /* 2950 */
    x  = p[-1];                         /* 2950 */
    x  = *(p + (-1));                   /* 2950 */

    pi = p - 1;                         /*      */
    pi = p - (-1);                      /* 2950 */
    pi = p + (-1);                      /* 2950 */
}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents