Posts: 7
Threads: 3
Joined: Aug 2015
Reputation:
0
It is hard to understand the reason to add Amplification 2 "The conversion of the constant expression in a switch statement's case label to the promoted type of the controlling expression".
What kind of risks should be eliminated with this?
Thank you,
Mario Ikeda
<t></t>
Posts: 7
Threads: 3
Joined: Aug 2015
Reputation:
0
Thank you. But I think unreachable code should be detected by Rule 2.1.
What confuses me is the phrase "The conversion .. to the promoted type of the controlling expression".
In this sample code, x is promoted to int(int16_t or int32_t) and -1 is converted to int.
As a result int is assigned to int. It should be compliant.
I may understand if the phrase is "The conversion .. to the essential type of the controlling expression".
<t></t>
Posts: 7
Threads: 3
Joined: Aug 2015
Reputation:
0
Sorry for late reply.
In my understanding both operands are promoted to signed int based on the Integral promotion rule.
When a cotrol expression is "x", case -1: is not reachable.
But if the control expression is "x-1", case -1: is reachable when x is 0.
I still do not understand what kind of risk should be eliminated by the Amplification 2.
Essential type should be discussed in this rule instead of "The conversion .. to the promoted type".
<t></t>
Posts: 7
Threads: 3
Joined: Aug 2015
Reputation:
0
Sorry for late reply.
In my understanding both operands are promoted to signed int based on the Integral promotion rule.
When a cotrol expression is "x", case -1: is not reachable.
But if the control expression is "x-1", case -1: is reachable when x is 0.
I still do not understand what kind of risk should be eliminated by the Amplification 2.
Essential type should be discussed in this rule instead of "The conversion .. to the promoted type".
<t></t>
Posts: 632
Threads: 18
Joined: Jan 2006
Reputation:
1
First some background on the C standard conversions in switch statements.
Quote:C99 6.8.4.2(5) The integer promotions are performed on the controlling expression. The constant expression in each case label is converted to the promoted type of the controlling expression.
The Amplification is written in a way which describes the relevent conversion in the C standard.
The actual check that should be performed is between the
essential type of the case label expression and the
essential type of controlling expression.
We agree that the wording is not clear and will clarify it in a later version.
In the example given by dg1980, "x" will be promoted from uint8_t ( assumed to be unsigned char ) to the C standard type of "signed int" if all the values of "unsigned char" fit in the "signed int" ( assumed to be 32-bit). The C standard type of "-1" is "signed int".
This is not compliant with rule 10.3. It will also violate rule 2.1 as suggested.
Posted by and on behalf of the MISRA C Working Group