[previous] 2910 [next] Constant: Wraparound in unsigned arithmetic operation.
Overflow and wraparound

An arithmetic operation is being performed in an unsigned integer type on constant operands and the result will be truncated because of wraparound. Is this intended ? The result is well-defined but may not be what was expected. Perhaps the message is being generated because the size of the integer type has not been configured appropriately in QA·C ?

Wraparound can occur in addition or multiplication if the result is too large to be represented. It can also happen in a subtraction operation if the value of the right hand operand is larger than the value of the left hand operand. Subtracting constants of unsigned type will never produce a negative result.

When wraparound occurs, the value which results is obtained by repeatedly adding or subtracting a value which is one more than the maximum value that can be represented in the unsigned type.

For example:


/*PRQA S 1278,2017,3120,3211,3302,3303,3304,3408 ++*/
/***************************************************/
/* OPTIONS: -s int=16                              */
/***************************************************/

unsigned int a = 10U - 20U;                    /* 2910 */
unsigned int b=  65530U + 5U;                  /*      */
unsigned int c=  65530U + 6U;                  /* 2910 */
unsigned int d=  30000U * 2U;                  /*      */
unsigned int e=  30000U * 3U;                  /* 2910 */

See also:

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