![]() |
![]() |
2802 | ![]() |
||||
![]() | |||||||
| Overflow and wraparound | |||||||
An apparent anomaly has been detected. Preceding operations imply that the value of the objects in this expression will sometimes be such as to cause overflow. For example, in the code shown below, the relational expression in the "if" statement implies that the value of n is expected to exceed 10000 - sometimes. If the size of an int is only 16 bits, overflow will result when n is multiplied by 4 - because 40000 cannot be represented in 16 bit signed arithmetic. Of course, if the value of n never exceeds 8191, the result of the multiplication will always be computed correctly; but this would imply that the "if" statement is redundant and any code contained in the "if" block is also redundant.
/*PRQA S 2017,2983,2986,3120,3199,3203,3227,3408 ++*/
/*****************************************/
/* OPTIONS: -s int=16 */
/*****************************************/
void foo(int n)
{
int x;
if (n > 10000)
{
}
x = n * 4; /* 2802 */
}
MISRA C:2012 Rules applicable to message 2802:
| Dir-4.1 (Required) | Run-time failures shall be minimized |
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index | Contents |