>
>
>
V2613. MISRA. Operand that is a composi…


V2613. MISRA. Operand that is a composite expression has more narrow essential type than the other operand.

This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) manual for software development.

This rule only applies to C. The analyzer has detected a situation: a composite expression participates in an arithmetic operation. This expression has more narrow essential type than another operand. Calculating this compound expression may lead to an overflow.

Let's look at the following synthetic example:

uint16_t w1;
uint16_t w2;
uint32_t dw1;
// ....
return w1 * w2 + dw1;

On typical platforms (x86/ARM) the 'uint16_t' type corresponds to the 'unsigned short' type. During the evaluation, 'unsigned short' expands to the 'int' type. However, on other platforms (for example, 16-bit microcontrollers), 'uint16_t' may correspond to the 'unsigned int'. Thus, there is no expansion to 32 bit, which may result in overflow in the multiplication.

The diagnostic can determine this via an essential type model. This model determines the expression type in such a way as if the expression didn't expand to 'int' (integer promotion). In this model, a variable may have the following types:

  • Boolean, if it operates with boolean values true/false: '_Bool';
  • signed, if it operates with signed integers or if it's an unnamed enum: 'signed char', 'signed short', 'signed int', 'signed long', 'signed long long', 'enum { .... };';
  • unsigned, if it operates with unsigned integers: 'unsigned char', 'unsigned short', 'unsigned int', 'unsigned long', 'unsigned long long';
  • floating, if it operates with floating-point numbers: 'float', 'double', 'long double';
  • character, if it operates only with characters: 'char';
  • Named enum, if it operates with a named set of user-defined values: 'enum name { .... };'.

To fix the situation, cast one of the composite expression operands to the resulting type. For example:

return (uint32_t)w1 * w2 + dw1;

Thus, the calculation of the expression occurs in a broader type 'uint32_t'.

This diagnostic is classified as:

  • MISRA-C-10.7