MISRA Discussion Forums
Clarification of Composite Expressions and casts - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.10 The essential type model (https://forum.misra.org.uk/forumdisplay.php?fid=165)
+---- Thread: Clarification of Composite Expressions and casts (/showthread.php?tid=1134)



Clarification of Composite Expressions and casts - rgamble - 03-12-2014

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?


Re: Clarification of Composite Expressions and casts - misra-c - 19-12-2014

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.