[previous] 2902 [next] Apparent: Positive integer value truncated by implicit conversion to a smaller unsigned type.
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 */
}


MISRA C:2012 Rules applicable to message 2902:

Rule-10.3  (Required) The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category.


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