MISRA Discussion Forums

Full Version: Recommendations to resolve issue 12.4
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
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]if(commData==0x24u)
{
if(CommandState>=7u)
{
if(CommandState
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:
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:
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))