else statement about Rule2.1

Moderators: misra-c, david ward

Post Reply
KumikoItoh
Posts: 6
Joined: Thu Feb 20, 2014 6:48 am
Company: HitachiAutomotiveSystems

else statement about Rule2.1

Post by KumikoItoh » Tue Oct 28, 2014 6:57 am

I have a question about Rule2.1.

Are these samples correct?
The else statement is null statement or not.

sample_1
if ( a<5 ) {
y=2;
} else if ( a>= 5 ) {
y=3;
}else{
y=0; /* not compliant */
}

sample_2
if ( a<5 ) {
y=2;
} else if ( a>= 5 ) {
y=3;
}else{
; /* compliant */
}

I think if the else statement is null statement the code is reachable.
Is is correct?

And how about is default label?

In case all case is coded,if default label has null statement , the code is reachable.
Is it correct?

Please teach me.

Regards

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

Re: else statement about Rule2.1

Post by misra-c » Fri Nov 14, 2014 9:51 am

Thank you for your query. We will reply after our next working group meeting which is on the 3rd December.
---
Posted by and on behalf of
the MISRA C Working Group

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

Re: else statement about Rule2.1

Post by misra-c » Fri Dec 05, 2014 8:36 am

The following response assumes that "a" is not volatile.

A statement with no executable code is not considered to be unreachable. This includes null statements and empty compound statements. The same applies to a default statement which contains no executable code.

The "else { ; } " is therefore not considered as unreachable code and is compliant with this rule.

The "else { y = 0; }" is unreachable as the statement contains code that would be executed if that control flow path had been reached.

However the above code does violate rule 14.3, since the condition "a >= 5" is an invariant as it will always evaluate to true due to the previous condition "a < 5 ".
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.2 Unused code”