Coverity and C++: heap (with new) vs. on-stack allocation

孤街醉人 提交于 2020-01-04 16:53:09

问题


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

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.

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).

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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/14934911/coverity-and-c-heap-with-new-vs-on-stack-allocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!