V2596. MISRA. The value of a composite expression should not be assigned to an object with wider essential type.
This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.
This diagnostic rule is relevant only for C.
The C language allows much flexibility in conversion between arithmetic types, but it also may lead to hidden problems such as loss of sign, loss of value, or loss of precision.
The code example:
void foo()
{
....
uint16_t var_a = 30000;
uint16_t var_b = 40000;
uint32_t var_sum;
var_sum = var_a + var_b; /* var_sum = 70000 or 4464? */
....
}
When the var_sum variable value is evaluated, the uint16_t type is implicitly converted to int. In consequence, the assignment result depends on the int type size:
- If
intis 32 bit, the modulo2^32operation is performed, and the expected70000value is written to thevar_sumvariable. - If
intis 16 bit, the modulo2^16operation is performed, and the70000 % 65536 == 4464value is be written to thevar_sumvariable.
The MISRA C standard defines its own type model, called the essential type model.
Using the essential type model can help avoid many non-obvious issues mentioned. In particular, it discourages assigning composite expressions that have a narrower essential type to variables of a wider essential type, or passing such expressions to a function as an argument of a wider type.
To fix the code above, use an explicit conversion to uint32_t:
void foo()
{
....
uint16_t var_a = 30 000;
uint16_t var_b = 40 000;
uint32_t var_sum;
var_sum = (uint32_t)var_a + var_b; /* var_sum = 70 000 */
....
};
Now the modulo 2^32 operation is performed in all cases, no matter what size the int type has, and the error does not occur even if int has the 16-bit size.
This diagnostic is classified as:
|