Recommendations to resolve issue 12.4

6.12 Expressions

Moderators: misra-c, david ward

Post Reply
jbrookley
Posts: 12
Joined: Thu Aug 08, 2013 5:56 pm
Company: Kavlico

Recommendations to resolve issue 12.4

Post by jbrookley » Wed Feb 25, 2015 10:16 pm

Hello,

I had a question regarding rule 12.4 and realize I asked a similar question here:

http://www.misra-c.com/forum/viewtopic.php?f=68&t=1296

I didn't want to bump an old thread so I started a new one (hopefully, that's ok).

I received an error for rule 12-4 with the following section of code:

Code: Select all

if((CommandState!=7u)&&(CommandState!=8u)&&(CommandState!=9u)&&(CommandState!=10u)&&(CommandState!=11u)&&(commData==0x24u)) /* Please ignore the fact this is written inefficiently for now */
{
		CommandState = 0u;
}
I resolved this issue using the following section of code but I wasn't sure if this is the best way to handle the code or if it's recommended to handle it using a different technique:

Code: Select all

if(commData==0x24u)
{
	if(CommandState>=7u)
	{
		if(CommandState<=11u)
		{

		}
		else
		{
			CommandState = 0u;
		}
	}
	else
	{
		CommandState = 0u;
	}
}
else
{

}
Doing the above will solve the expression rule but it seems it would add additional "complexity", of which there are also rules against, if I remember correctly so I would be potentially getting rid of one problem and adding another way. Is this the recommended way to handle multiple case comparisons or is there another method to resolve rule violations like this? Or would I want to make a separate if statement for each individual case (adding more code space but reducing complexity and the expression with side effects errors).

Let me know when you get the chance. Thanks!

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

Re: Recommendations to resolve issue 12.4

Post by misra-c » Fri Mar 13, 2015 8:35 am

Your first code example will only violate rule 12.4 if CommandState and/or commData have volatile qualified types.

You would violate no rules by restructuring your code with multiple "if statements". However you should be aware that if CommandState is volatile qualified that its value could change between the tests in both versions of the code.

Two alternatives ways of writing the example would be either with a switch statement:

Code: Select all

switch ( CommandState )
{
   case 7U:
   case 8U:
   case 9U:
   case 10U:
   case 11U:
   {
      break;
   }
   default:
   {
      if ( commData == 0x24U )
      {
         CommandState = 0U;
      }
      break;
   }
}
Or by only accessing any volatiles once by use of a temporary object:

Code: Select all

   uint32_t CommandState_hold;  /* or appropriate type */
   uint32_t commData_hold;
   CommandState_hold = CommandState;
   commData_hold = commData;
  
   if(  (CommandState_hold!=7u)&&(CommandState_hold!=8u)&&(CommandState_hold!=9u)
        &&(CommandState_hold!=10u)&&(CommandState_hold!=11u)&&(commData_hold==0x24u))
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.12 Expressions”