[previous] 2906 [next] Definite: Positive integer value truncated by cast to a smaller unsigned type.
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 behavior 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 */
    }
}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents