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

A definite 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 always 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. If the value of si is less than 0 or greater than 31 the resulting behaviour is undefined.


/*PRQA S 1253,1277,1279,1575,1594,2017,2983,2986,3120,3198,3199,3203,3227,3408,4131 ++*/
void f1(unsigned long ul, int si)
{
    if (si > 40)
    {
        ul = ul << si;                /* 2791 */
    }
}

void f2(unsigned long ul, int si)
{
    if (si < 0)
    {
        ul = ul << si;                /* 2791 */
    }
}


MISRA-C:2004 Rules applicable to message 2791:

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