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

Dataflow analysis has identified a code construct which is suspicious. The code may be entirely safe but further investigation is advisable.

Examination of the immediate context suggests that the value of the right hand operand in this shift operation (i.e. the number of bits to be shifted) could sometimes be 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.

For example, in the code below, the result of the shift operation will be undefined if the value of the parameter 'n' is zero or negative.


/*PRQA S 2017,2983,3120,3199,3227,3408,4131 ++*/

void foo(unsigned long ul, int n)
{
    int si = 40;
    int i;

    for (i = 0; i < n; ++i)
    {
        --si;
    }

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


No MISRA C:2012 Rules applicable to message 2793


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