Coverity和C ++:heap(with new)与堆栈分配 [英] Coverity and C++: heap (with new) vs. on-stack allocation

查看:269
本文介绍了Coverity和C ++:heap(with new)与堆栈分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 coverity (5.5.1)(其他)来强化我的代码。我偶然发现了一个问题,现在我怀疑我的设置是不完整的。看看这个例子:

I'm using coverity (5.5.1) (among others) to harden my code. I stumbled over a problem and now I have doubts that my setup of coverity is not complete. Have a look at this example:

class A
{
    int _a,_b;
public:
    A(int b) : _a(_b), _b(b)
    { }
};

int main(void)
{
    A *a1 = new A(5);
    delete a1;

    A a2(5);
    return 0;
}

可以看出,我使用 _b 在用 b 初始化之前初始化 _a 。在中问题我知道,编译器或任何其他工具发出这样的警告将是很好。

As can be seen I'm using _b to initialize _a before it is initialized with b. In this question I learned that it would be "nice to have" such a warning issued by the compiler or any other tool.

与我原先对这个问题的理解相反,我现在发现( UNINT ),但是只有当在堆栈上分配时,而不是在使用 new 创建的时候。所以在我的 main - 功能我得到 A a2(5)的警告,但不是 A * a1 = new A(5)

Contrarily to my original understanding of the problem I now found out, that coverity actually emits a defect which matches perfectly (UNINT), but only when allocated on the stack, not when created with new. So in my main-function I get the warning for A a2(5) but not for A *a1 = new A(5).

在我看来,coverity正在处理对构造函数的调用, code> new 比在堆栈上创建的对象。

It seems to me that coverity is handling the call to the constructor different when using new than when the object created on the stack.

在我的封面配置中有没有什么我忽略了?

Is there anything I overlooked in my coverity configuration? What can I do to have the warning when allocated on the heap?

推荐答案

事实证明,根据支持,这是一个Coverity中的已知错误(即使在当前版本中)。

It turned out that according to the support this is a known bug in Coverity (even in the current version). A fix might arrive with the version released the following summer.

错误在ID下列出:50128 UNINIT FN:在初始化之前在构造函数中使用的成员

The bug is listed under the ID: 50128 UNINIT FN: member used in constructor before initialization and will as such be put into the release-notes when fixed and released.

这篇关于Coverity和C ++:heap(with new)与堆栈分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆