Clarification of Composite Expressions and casts

Moderators: misra-c, david ward

Post Reply
Posts: 14
Joined: Fri Jul 20, 2007 7:54 pm

Clarification of Composite Expressions and casts

Post by rgamble » Wed Dec 03, 2014 2:45 pm

In the example below, is the cast to uint32 in the statement in which r2 is assigned a violation of Rule 10.8?

Code: Select all

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?

Posts: 572
Joined: Thu Jan 05, 2006 1:11 pm

Re: Clarification of Composite Expressions and casts

Post by misra-c » Fri Dec 19, 2014 1:13 pm

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.
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.10 The essential type model”