V2543. MISRA. Value of the essential character type should be used appropriately in the addition/subtraction operations.
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.
According to the essential type model, essential character type values should not be used in arithmetic expressions, since they are represented as a non-numerical.
Here is the list of correct ways of using character-type variables in arithmetic expressions:
- Addition: one operand should have a character type, another one should have signed or unsigned integer type. The result of such operation has the character type:
character+[un]signed=>character[un]signed+character=>character
- Subtraction: the left operand should have a character type, and the right operand should have signed or unsigned integer type. The result of this operation will be a value of character type:
character-[un]signed=>character
- Subtraction: both operands should have a character type. The result of the operation will be a value of signed integer type:
character-character=>signed
The example:
void foo(char ch, unsigned ui, float f, _Bool b, enum A eA)
{
ch + f; // Essential character type should not be used in
// the addition operation with expression
// of the essential floating type
ch + b; // Also relates to the essential Boolean
ch + eA; // Also relates to the essential enum <A> type
(ch + ui) + (ch - 6); // After the expressions in parentheses
// have been executed, both operands of the
// essential character type are used
// in addition operation
}
This diagnostic is classified as:
|