Rule 10.6 An expression shall not be assigned to a wider type

Moderators: misra-c, david ward

Post Reply
LordMordac
Posts: 2
Joined: Tue Jan 14, 2020 8:05 pm
Company: nvidia

Rule 10.6 An expression shall not be assigned to a wider type

Post by LordMordac » Mon Jan 27, 2020 11:47 pm

The full rule title is:
The value of a composite expression shall not be assigned to an object with a wider essential type
I am having a lively debate with my scanner vendor about the quality of their scans. The following example code is generating the commented violation:

Code: Select all

static inline uint32_t bit32 (uint32_t pos)
{
    static const uint32_t mask = 31U;
    static const uint32_t one = 1U;

    // Event misra_c_2012_rule_10_6_violation: Assigning composite expression "1U << (pos & 0x1fU)" of width 8 to a target of width 32.
    return one << (pos & mask);
}
The whole argument centers around the UTLR of the variable one. Their claim is that according to the spec the variable one has a UTLR of unsigned char, and the violation is real. Please advise.

misra-c
Posts: 564
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 10.6 An expression shall not be assigned to a wider type

Post by misra-c » Mon Feb 03, 2020 1:53 pm

Appendix D.7 gives the essential type of an Shift Expression. It says:
Shift( << >> )
1. If the left hand operand is essentially unsigned then:
1.1 If both operands are integer constant expressions then the essential type of the result is the UTLR of the result;
1.2 Else the essential type of the result is the essential type of the left hand operand
In this case 1.2 applies and essential type is that of the left hand operand. The essential type of the left hand operand is uint32_t. There is therefore no assignment to a wider essential type and no violation of rule 10.6.

There would have been a violation of rule 10.6 if the code had been written as

Code: Select all

  1U << ( pos & mask );
since the essential type of the left hand operand of the << operator would have been "unsigned char".

Variables which are declared "const" do not have an essential type which depends on the value.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.10 The essential type model”