, 已知为 nullptr",c++,static-analysis,coverity"/>

首页 > 解决方案 > 静态分析给出“取消引用, 已知为 nullptr"

问题描述

我在一个项目上使用 Coverity 运行静态分析,而我遇到了该工具报告的这个错误:

Dereferencing <storage from new>, which is known to be nullptr

代码片段是:

typedef struct envelope
{
      char *message;
      void *context;
      bool response;
}envelopeRef;

int main()
{
   /* here coverity tells that
      1. returned_null: operator new returns nullptr (checked 46 out of 52 times)
      2. var_assigned: Assigning: <storage from new> = nullptr return value from operator new
      3. dereference: Dereferencing <storage from new>, which is known to be nullptr.
   */
    envelopeRef *h = new (std::nothrow)envelopeRef();
    if(nullptr != h)
    {
       //do something
       delete h;
    }
}

我在这里做错了什么?

标签: c++static-analysiscoverity

解决方案


推荐阅读