![]() |
![]() |
2902 | ![]() |
||||
![]() | |||||||
| Conversion to unsigned | |||||||
An apparent anomaly has been detected. The value of an integer expression is being implicitly converted to an unsigned type. Preceding operations suggest that the value of this expression will sometimes be larger than the maximum value which can be represented in this type and so truncation will occur using modulo arithmetic. In the example below, the presence of the "if" statement implies that values of
uca in excess of 200 are expected.
If this happens, the result of the multiplication will exceed 400 and truncation will occur in the assignment operation.
Of course, if uca never exceeds 127, truncation will not occur - but this would imply that the "if" statement, and any code which it contains, are both redundant.
/*PRQA S 1251,1278,2017,2100,2982,2983,2984,2986,3120,3198,3199,3203,3227,3297,3408 ++*/
void foo(unsigned char uca)
{
unsigned char ucr;
if (uca > 200U)
{
}
ucr = uca * 2; /* 2902 */
ucr = (unsigned char)(uca * 2); /* 2907 */
}
See also:
![]() | ||
| QA·C Source Code Analyser 8.1.2
© 2013 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | Contents |