如何在 Parasoft C++test 中将函数标记为非返回?
我们有一个 die
函数,它输出错误消息并退出,例如:
void die(const char* msg) {
fprintf(stderr, "Error: %s\n", msg);
exit(1);
}
我们使用 Parasoft C++test 静态分析我们的代码,但它没有意识到 die
是一个非返回函数。因此,当它看到如下代码时:
void foo(Bar* bar) {
if(!bar) {
die("bar is NULL");
}
Bar bar2 = *bar;
}
它会警告 *bar
可能会取消引用空指针,即使 bar
为 NULL 会阻止该行执行。有没有办法以 Parasoft 能够识别的方式将 die
标记为不返回?
编辑:我需要一些可以在 GCC 和 VS 2003 中工作的东西,但如果有人有一个只能在VS
We have a die
function that outputs an error message and exits, e.g.:
void die(const char* msg) {
fprintf(stderr, "Error: %s\n", msg);
exit(1);
}
We use Parasoft C++test to statically analyze our code, but it doesn't realize that die
is a non-returning function. So when it sees code like:
void foo(Bar* bar) {
if(!bar) {
die("bar is NULL");
}
Bar bar2 = *bar;
}
It warns that *bar
might be dereferencing a null pointer, even though bar
being NULL would prevent that line from ever executing. Is there a way to mark die
as non-returning in a way Parasoft would recognize?
Edit: I need something that works in both GCC and VS 2003, but I'm not above #ifdef
ing my way around things if somebody has a solution that only works in VS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想通了。事实证明,Parasoft 有一个内置的 NRF 列表,您可以自定义;它们被称为“终止函数”。 运行 Parasoft,则可以通过 VS GUI 或配置文件来编辑它们
如果您在 VS 之外通过 Visual Studio
C++test
菜单中打开Test Configurations
对话框Static
选项卡BugDetective Options
子选项卡选择
Terminators
子选项卡窗口截图 http://so.mrozekma.com/parasoft1.png
添加具有非返回函数名称的新条目
条目截图 http://so.mrozekma.com/parasoft2.png
通过配置文件
添加如下行:
I figured it out. It turns out Parasoft has a built-in list of NRFs you can customize; they're called "terminating functions". You can edit them through the VS GUI or through the configuration file if you run Parasoft outside of VS
Through Visual Studio
Test Configurations
dialog from theC++test
menuStatic
tabBugDetective Options
subtabSelect the
Terminators
sub-subtabScreenshot of the window http://so.mrozekma.com/parasoft1.png
Add a new entry with the non-returning function name
Screenshot of the entry http://so.mrozekma.com/parasoft2.png
Through the configuration file
Add lines like the following:
在 gcc 中,您应该为该函数添加如下属性:
In gcc, you should attribute the function with something like:
如果您使用的是 Visual Studio 2005+,则可以使用
__declspec(noreturn)
像这样:也许这有助于 Parasoft 将该函数识别为非返回函数。
编辑: GCC 有
__attribute__((noreturn))
(第一个示例)。If you're using Visual Studio 2005+, you can use
__declspec(noreturn)
like this:Maybe that helps Parasoft to recognize the function as non-returning.
Edit: GCC has
__attribute__((noreturn))
(first example).