![]() |
![]() |
2897 | ![]() |
||||
![]() | |||||||
| Conversion to unsigned | |||||||
An apparent anomaly has been detected. The value of an object or a non-constant expression is cast to an unsigned type and preceding operations suggest that the value will sometimes be negative. Is this really intended ? The operation will be well defined but is likely at best to be confusing. The unsigned value which results will the be the value obtained by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. For example, in the following code, if char is an 8 bit type and the value of
sia is negative, the value assigned to ucx will be the value obtained by repeatedly adding 256 to the value of sia until a non-negative value is achieved..
/*PRQA S 2017,2983,2986,3199,3203,3227,3408 ++*/
void foo(int sia)
{
unsigned char ucx;
if (sia < 0)
{
}
ucx = (unsigned char)sia; /* 2897 */
}
MISRA-C:2004 Rules applicable to message 2897:
| Rule 21.1 (Required) | Minimisation of run-time failures shall be ensured by the use of at least one of (a) static analysis tools/techniques; (b) dynamic analysis tools/techniques; (c) explicit coding of checks to handle run-time faults |
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index | Contents |