4

在测试我的代码(静态分析)以查看我是否尊重 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 的不合规。

感谢您的意见。

罗尼。

4

1 回答 1

3

以下代码不应报告应用 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 检查器检查上面的代码段,看看它是否仍在报告任何内容。如果问题仍然存在,我会联系工具供应商并询问他该问题。

于 2013-12-06T14:18:20.260 回答