[previous] 2792 [next] Apparent: Right hand operand of shift operator is negative or too large.
Shift operations

An apparent anomaly has been detected. Examination of the context indicates that the value of the right hand operand (i.e. the number of bits to be shifted) is sometimes out of range. According to the ISO-C standard if the value is either negative or greater than or equal to the width in bits of the left hand operand (after integral promotion), behaviour is undefined.

In the code below, the size of a long is assumed to be 32 bits. Unless the if statement is redundant, the code implies that the value of si is expected to exceed 40 sometimes - and if it does, behaviour of the subsequent shift operation will be undefined.


/*PRQA S 1253,1277,1279,1575,1594,2017,2983,2986,3120,3198,3199,3203,3227,3408,4131 ++*/

void foo(unsigned long ul, int si)
{
    if (si > 40)
    {
    }

    ul = ul << si;                    /* 2792 */
}


MISRA-C:2004 Rules applicable to message 2792:

Rule  21.1  (Required) Minimisation of run-time failures shall be ensured by the use of at least one of (a) static analysis tools/techniques; (b) dynamic analysis tools/techniques; (c) explicit coding of checks to handle run-time faults


See also:

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