![]() |
![]() |
2906 | ![]() |
||||
![]() | |||||||
| Conversion to unsigned | |||||||
A definite anomaly has been detected. The value of an integer expression is being cast to an unsigned type. Examination of the context indicates that the value will always be greater that the maximum value which can be represented in this type and so truncation will occur using modulo arithmetic. In the example below, we know that the value of the expression will always be greater than 400. However an object of type unsigned char can typically only represent values between 0 and 255. The value assigned to
ucr will actually be (uca * 2) % 256. If this is the intended behaviour it would be better to code it explicitly.
/*PRQA S 1251,1278,2017,2100,2982,2983,2984,2986,3120,3198,3199,3203,3227,3296,3408 ++*/
void foo(unsigned char uca)
{
unsigned char ucr;
if (uca > 200U)
{
ucr = uca * 2; /* 2901 */
ucr = (unsigned char)(uca * 2); /* 2906 */
}
}
No MISRA C:2012 Rules applicable to message 2906
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 |