[previous] 2913 [next] Suspicious: Wraparound in unsigned arithmetic operation.
Overflow and wraparound

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

An arithmetic operation is being performed in an unsigned type and examination of the immediate context suggests that the value of the expression could sometimes be too large to be representable in this type. If so, wraparound will occur and the result will be truncated using modulo arithmetic. Is this intended ?

This will happen in the example below unless the parameter 'n' is greater than 0.


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

   for (i = 0; i < n; ++i)
   {
      un = un / 10U;
   }

   un = un * 7U;                  /* 2913 */
}


No MISRA-C:2004 Rules applicable to message 2913


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