Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recommendations to resolve issue 12.4
#2
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))
Posted by and on behalf of the MISRA C Working Group
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 3 Guest(s)