MISRA C:1998 Rule 58

Questions and discussions about MISRA C not fitting under migration or rules forums

Moderators: misra-c, david ward

Post Reply
himamsu
Posts: 1
Joined: Thu Jun 28, 2007 10:03 am
Company: Mistral Solutions Pvt Ltd
Location: Bangalore, India

MISRA C:1998 Rule 58

Post by himamsu » Fri Jun 29, 2007 4:11 pm

First, its good to see a forum on MISRA C.

I was trying to understand rule 58 (break in a switch statement) from a control flow perspective. Here is a code snippet.

Code: Select all

FUNC_RESULT aFunction(void)
{
FUNC_RESULT result;
......
......
.......

switch(x)
            {
            case 0:
                  result = checkInitVars();
                   /* If checkInitVars is a success, then call 
                    * the startOperation too. Else, break. */
                  if (SUCCESS != result) 
                     {
                      /* something not fine here, get out. */
                      break; /* Is the break here OKAY? */
                      }
                 
                 /* Initialization is fine, start the job! */ 
                 result = startOperation();
                 break;

            case 1:
                  result = checkInitVars();
                  if (SUCCESS == result) 
                     {
                      result = startOperation();
                      }
                  break;
            
             default:
                    result = FAILED;
                    break;
            }
.....
.....
.....
return (result);
}
I would not want to call 'startOperation()' if my initialization variables are checked and returned to have correct values. In both the cases, the operation is started, only if the variables are correct. In light of that, I would like to know which of the case statement is okay and which are not according to the rule.

Thanks in advance,
Himamsu.

phaedsys
Posts: 50
Joined: Fri Sep 17, 2004 10:53 pm
Company: Phaedrus Systems
Location: Phaedrus Systems Tamworth, UK
Contact:

Re: MISRA C:1998 Rule 58

Post by phaedsys » Sun Jul 01, 2007 12:05 pm

I would suggest the following.

Code: Select all

switch(x)
            {
            case 0:
                result = checkInitVars();
                if (SUCCESS != result) 
                {
                      /* something not fine here, get out. */
                 }
                 else
                {
                      result = startOperation();
                 } 
                break;

            case 1:
                  result = checkInitVars();
                  if (SUCCESS == result) 
                     {
                      result = startOperation();
                      }
                  break;
            
             default:
                    result = FAILED;
                    break;
            }
case 0 uses if-else. so you can catch a specific \"if\" and all esle will be the oposite state.

depending on you system your default should return an error or place the system in a safe state.

Why are you switching on X and then testing for result? I think you could have a more elegant solution.
This post is made in a personal capacity
Member of MISRA-C-WG since 2002

Post Reply

Return to “General Questions”