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

The value of an integer expression is being added to or subtracted from a pointer. Examination of the context indicates that this value will sometimes be negative and 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,491,492,510,2017,2211,2824,2982,2983,2984,2986,3120,3132,3140,3198,3199,3203,3227,3408,3447,3673,3690 ++*/

void f1(int *p, int n)
{
    int r;

    if (n < 0)
    {
        ;
    }

    r = p[n];               /* 2952 */
    r = *(p + n);           /* 2952 */
    r = *(p - n);           /* 2952 */

}

void f2(int *p, int n)
{
    int r;

    if (n > 0)
    {
        ;
    }

    r = p[n];               /*      */
    r = *(p + n);           /*      */
    r = *(p - n);           /*      */

}


No MISRA C:2012 Rules applicable to message 2952


See also:

QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents