[previous] 2822 [next] Apparent: Arithmetic operation on NULL pointer.
NULL pointers

An apparent anomaly has been detected. Preceding operations imply that this pointer value will sometimes be null.

The internal representation of a null pointer is implementation-defined and may not necessarily be bitwise 0. A null pointer may be assigned or compared, but any attempt to perform arithmetic operations on it will result in undefined behavior.

For example, in the code shown below, the expression in the "if" statement implies that the value of q is expected to be NULL - sometimes, and so it should not be incremented. Of course, if the value of q is never actually NULL, the increment operation will always be valid; but this would imply that the "if" statement is redundant and any code contained in the "if" block is also redundant.


/*PRQA S 489,508,509,2017,3205,3408,3673,4600 ++*/
#define NULL (void *)0

void foo(int *q)
{
    int *p;

    if (q == NULL)
    {
    }

    ++q;                    /* 2822  */
}

See also:

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