gpt4 book ai didi

c++ - 米斯拉-C++ :2008[8-4-3] : return in all exit path in function

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:02:56 25 4
gpt4 key购买 nike

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

Function does not return a value on all paths.

函数看起来像

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

关于c++ - 米斯拉-C++ :2008[8-4-3] : return in all exit path in function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com