MISRA-C++:2008[8-4-3]:返回函数中的所有出口路径

MISRA-C++:2008[8-4-3] : return in all exit path in function

本文关键字:出口 路径 返回 2008 8-4-3 MISRA-C++ 函数      更新时间:2023-10-16

在测试我的代码(静态分析)以查看我是否尊重misra c++2008时,我得到以下错误

函数不会在所有路径上返回值

功能看起来像

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

我真的需要抛出一个异常,因为根据错误的不同,调用者会做一些事情。可能的错误列表可能很大,这不仅仅是因为该值不存在,就像这个代码示例中一样。

我该怎么办?我认为在这种情况下,我使用的工具不应该将其视为不符合misra。

谢谢你的建议。

罗尼。

以下代码不应报告应用了MISRA C++2008规则的任何警告/错误。因此,很可能是您的工具出现了问题,或者发布的代码不是受影响的部分。

#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;
}

试着用你的MISRA检查器只检查上面的片段,看看它是否仍在报告任何内容。如果问题仍然存在,我只会联系工具供应商,询问他有关该问题的情况。