4

When testing my code (static analysis) to see if i respect misra c++ 2008, i get the following error

Function does not return a value on all paths.

The function looks like

int* Dosomething(string v)
{
   int* retvalue = NULL;

   if( 0 == exists(v) )
   {
      throw("error: value doesn't exist");
   }
   else
   {
     retvalue = dosomecomputations(v);
   }

   return retvalue;
}

I really need to throw an exception, because depending of the error the caller shall do something. The possible list of errors can be big and it is not just that the value doesn't exist as in this sample of code.

How can i manage it? I think that in this case the tool i'm using should not see it as a non-compliance to misra.

Thanks for your advise.

rony.

Constantin
  • 8,721
  • 13
  • 75
  • 126
bonpiedlaroute
  • 173
  • 1
  • 9
  • 2
    Which MISRA checker are you using (QAC? PClint? ...)? In my opinion the snippet that you have posted doesn't violate Rule 8-4-3 – Constantin Dec 06 '13 at 13:32
  • looks like a bug of the tool – BЈовић Dec 06 '13 at 13:35
  • @constantin, i'm using ldra – bonpiedlaroute Dec 06 '13 at 13:43
  • Try adding bogus return after the throw. Also try removing `else`. – hyde Dec 06 '13 at 15:19
  • 1
    Out of curiousity, if you are using a modern static analyser, then why are you writing code using the obfuscated "Yoda conditions"? `0 == exists(v)` instead of `exists(v) == 0`? This way of writing code was invented by garage hackers about the time the Star Wars movies were released. It became obsolete in 1990 or so, when even the crappiest compilers managed to warn against assignment inside conditions. For a modern static analyser, finding such bugs will be a walk in the park. Is this 30 years old code? – Lundin Dec 10 '13 at 12:50

1 Answers1

3

The following code should not report any warnings/errors with MISRA C++ 2008 rules applied. So most likely it's an issue with your tool - or the posted code is not the affected part.

#include <string>

int exists(std::string v){ (void)v; return 1; }
int* dosomecomputations(std::string v){ (void)v; return NULL; }

int* dosomething(std::string v){
  int* retvalue = NULL;
  if( 0 == exists(v) ){
    throw("error: value doesn't exist");
  }else{
    retvalue = dosomecomputations(v);
  }
  return retvalue;
}

Try to check just the snippet above with your MISRA checker and see if it's still reporting anything. If the problem persists I would just contact the toolvendor and ask him about that issue.

Constantin
  • 8,721
  • 13
  • 75
  • 126
  • 2
    i have checked with the above snippet, and i still got the same error. My conclusion is that it is a bug in the tool. Thanks Constantin. – bonpiedlaroute Dec 06 '13 at 14:48