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) software development guidelines.
This diagnostic rule is relevant only for C.
The MISRA C standard defines its own type model, called the essential type model.
The analyzer has detected an issue: a composite expression is used in an arithmetic operation where this expression is narrower essential type than another operand. Evaluating this composite expression may lead to an overflow.
The 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, no expansion to 32 bit occurs, which may result in overflow in the multiplication.
To fix the issue, convert one of the composite expression operands to the resulting type.
The example:
return (uint32_t)w1 * w2 + dw1;
Thus, the expression is evaluated in a wider type, uint32_t
.
This diagnostic is classified as:
|