MISRA Discussion Forums

Full Version: Clarification of Composite Expressions and casts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In the example below, is the cast to uint32 in the statement in which r2 is assigned a violation of Rule 10.8?

Code:
typedef unsigned short uint16;
typedef unsigned int uint32;

void func(uint16 a, uint16 b) {
    uint32 r1;
    uint32 r2;
    r1 = (uint32) (a - b); // Violation of 10.8
    r2 = (uint32) (uint16) (a - b); // Violation of 10.8?
}

There do not seem to be any examples that specifically comment on this case but the wording of the definition of 'composite expression' introduces room for uncertainty. In particular, the part that says a composite expression
is the 'direct result of a composite operator'. What is the significance of 'direct' in the definition? Does (a - b) lose it's status as a composite expression after being cast to uint16 due to no longer being a 'direct result' of the composite operator? Or does 'direct' refer to something else? Finally, if this example is a violation of 10.8, is there a suggested means to avoid a violation without introducing a temporary variable?
You are correct in saying that the second example is not a violation of rule 10.8. The operand of the uint32 cast is the uint16 cast which is not in the list of composite operators.

When writing the MISRA C guidelines, the working group felt that the presence of the cast indicated that the programmer knew what type was expected and hence the issues covered in section 8.10.3 did not arise.