[previous] 2911 [next] Definite: Wraparound in unsigned arithmetic operation.
Overflow and wraparound

An arithmetic operation is being performed in an unsigned type. Examination of the context indicates that the result is always too large to be representable in this type and so wraparound will occur - the result will be truncated using modulo arithmetic. Perhaps this is intended ?

For example:


/*PRQA S 1278,2017,2982,2983,2984,2986,3120,3198,3199,3203,3227,3372,3408 ++*/
/*******************************/
/* OPTIONS: -s int=16          */
/*******************************/
void foo(unsigned int un)
{
    unsigned unr;

    if (un > 40000U)
    {
        unr = un * 7U;              /* 2911 */
        unr = un + un;              /* 2911 */
    }
    else
    {
        unr = un - 50000U;          /* 2911 */
    }
}


MISRA-C:2004 Rules applicable to message 2911:

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