[previous] MISRA C:2012  Rule-12.4:  (Advisory) [next] Evaluation of constant expressions should not lead to unsigned integer wrap-around

Amplification:

This rule applies to expressions that satisfy the constraints for a constant-expression, whether or not they appear in a context that requires a constant-expression.

If an expression is not evaluated, for example because it appears in the right operand of a logical AND operator whose left operand is always false, then this rule does not apply.

Example Code:


#pragma PRQA_MESSAGES_OFF 2982,2984

#include "misra.h"
#include "m3cmex.h"

#define BASE  65024u
#define DELAY 10000u
#define WIDTH 60000u

extern int16_t rule_1204( void )
{
   static const uint16_t cy = 0xffffu;

   u32a = 0x1U - 0x2U;                    /* 2910 */

   u32a = 0xFFFFFFFFU + 0x2U;             /* 2910 */
   u32a = 0xFFFFU + 0x2U;                 /* 2910 */
   u32a = 0xFFU + 0x2U;

   u32a = 0x0FFFFFFFU * 32U;              /* 2910 */
   u32a = 0x0FFFU * 32U;                  /* 2910 */
   u32a = 0x0FU * 32U;

   switch (u16a)
   {
   case BASE + 0u:   ++u16a; break;
   case BASE + 1u:   ++u16b; break;
   case BASE + 512u: ++u16r; break;       /* 2910 */
   default:                break;
   }

#if 1u + (0u - 10u)                       /* 2910 */
#endif

   u16a = DELAY + WIDTH;                  /* 2910 */

   u16b = cy + 1u;                        /*      */

   u16c = (0u == 0u) ? 0u : (0u - 1u);    /* 2910 */      /* Compliant - but operation is unreachable */

   u16d = bla ? 0u : (0u - 1u);           /* 2910 */

   return 0;
}


QAC messages that encompass this guideline:

2910 Constant: Wraparound in unsigned arithmetic operation.



(c) The Motor Industry Research Association, 2012
QA C Source Code Analyser 8.1.2
MISRA C:2012 Compliance Module 1.0
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents