[previous] 2908 [next] Suspicious: Positive integer value truncated by cast to a smaller unsigned type.
Conversion to unsigned

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

The value of an integer expression is being cast to an unsigned type. Examination of the immediate context suggests that the value of this expression could sometimes be larger than the maximum value which can be represented in this type and so truncation would occur using modulo arithmetic. Is this intended ?

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


/*PRQA S 1251,1278,2017,2100,2982,2983,2984,2986,3120,3198,3199,3203,3227,3297,3408 ++*/

void foo(int n)
{
   unsigned int  uia = 128U;
   unsigned char ucr;
   int           i;

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

   ucr = uia * 2U;                     /* 2903 3768 */
   ucr = (unsigned char)(uia * 2U);    /* 2908      */
}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents