facebook / infer

A static analyzer for Java, C, C++, and Objective-C
http://fbinfer.com/
MIT License
14.93k stars 2.01k forks source link

False positive of Infer/Pulse when `std::unique_ptr` is used with no throw `new` #1777

Closed Qiuye-Hua closed 1 year ago

Qiuye-Hua commented 1 year ago

Infer version: v1.1.0-d83ea2caec OS: Ubuntu 20.04 Running command: infer run --pulse g++ test.cpp


Consider the following simple program test.cpp:

#include <memory>

int main()
{
    std::unique_ptr<int> p1(new (std::nothrow) int);
    if (!p1) {
        return 1;
    }
    std::unique_ptr<int> p2(new (std::nothrow) int);
    if (!p2) {
        return 1;
    }
    return 0;
}

where std::nothrow makes sure that new does not throw exception when there is a problem, but return nullptr instead.

The thing is, Infer/Pulse seems to confuse this syntax with placement new operator, and emit a "Use After Delete" warning for it.

If I run Infer with infer run --pulse -- g++ test.cpp I got:

test.cpp:13: error: Use After Delete accessing memory that was invalidated by delete on line 13.

  1. return 1;
  2. }
  3. return 0; ^
  4. }
skcho commented 1 year ago

@Qiuye-Hua Thank you for the report with clear explanation/examples!

Qiuye-Hua commented 1 year ago

Thank you for the amazingly fast fix! After recompiling Infer with the latest code, the false positives in my project went away :thumbsup: .