llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.99k stars 11.95k forks source link

clang-analyzer-cplusplus.NewDeleteLeaks false positive in C++17 mode only #37524

Open 0x8000-0000 opened 6 years ago

0x8000-0000 commented 6 years ago
Bugzilla Link 38176
Version trunk
OS Linux
CC @gburgessiv,@kenr,@haoNoQ,@pirama-arumuga-nainar,@stephenhines,@svenpanne

Extended Description

florin@helios$ cat unique.cc

include

include

include

using Bar = uint64_t;

class Foo { public: explicit Foo(Bar bar) : bar(bar) { }

const Bar& getBar() { return bar; }

private: Bar bar; };

std::unique_ptr fooMaker() { Bar bar = {};

return std::make_unique(bar); }

int main() { return 0; }

florin@helios:$ clang++-6.0 --analyze -Xanalyzer -analyzer-output=text -std=c++14 -o unique.o unique.cc
florin@helios:$ clang++-6.0 --analyze -Xanalyzer -analyzer-output=text -std=c++17 -o unique.o unique.cc
florin@helios:$ /opt/llvm7/bin/clang++ --analyze -Xanalyzer -analyzer-output=text -std=c++14 -o unique.o unique.cc
florin@helios:$ /opt/llvm7/bin/clang++ --analyze -Xanalyzer -analyzer-output=text -std=c++17 -o unique.o unique.cc
Writing to last piece unique.cc:27:4: warning: Potential leak of memory pointed to by field '_M_head_impl' return std::make_unique(bar); ^ unique.cc:27:11: note: Calling 'make_unique<Foo, unsigned long &>' return std::make_unique(bar); ^~~~~~ /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/unique_ptr.h:831:30: note: Memory is allocated
{ return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); } ^~~~~~~~~~~ unique.cc:27:11: note: Returned allocated memory return std::make_unique(bar); ^~~~~~ unique.cc:27:4: note: Potential leak of memory pointed to by field '_M_head_impl' return std::make_unique(bar); ^ 1 warning generated.


This is with LLVM/Clang/Clang-Extra from trunk

llvm: commit 2d2ebb317ec78069296276ca13a8461e4a27e6c6 Author: Andrea Di Biagio Andrea_DiBiagio@sn.scee.net git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337124 91177308-0d34-0410-b5e6-96231b3b80d8

clang: commit 27d1a66dffa9424bcdadc7cd52cf2ae019e49889 Author: Aaron Ballman aaron@AaronBallman.com git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@337125 91177308-0d34-0410-b5e6-96231b3b80d8

efa41be3-5b15-484e-b29c-bb63fbb25564 commented 3 years ago

Any progress?

efa41be3-5b15-484e-b29c-bb63fbb25564 commented 4 years ago

Preprocessed file, made with clang++ --analyze -Xanalyzer -analyzer-output=text -std=c++17 -E test.cpp

haoNoQ commented 4 years ago

Ken, could you please attach a preprocessed file?

efa41be3-5b15-484e-b29c-bb63fbb25564 commented 4 years ago

I'm able to reproduce it with the example above using MSVC 14.16.27023 headers.

c:\tmp\z>clang++ --analyze -Xanalyzer -analyzer-output=text -std=c++17 test.cpp test.cpp:20:3: warning: Potential leak of memory pointed to by field '_Myval2' return std::make_unique(bar); ^ test.cpp:20:10: note: Calling 'make_unique<Foo, unsigned long long &, 0>' return std::make_unique(bar); ^~~~~~ c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\memory:2539:26: note: Memory is allocated return (unique_ptr<_Ty>(new _Ty(_STD forward<_Types>(_Args)...))); ^~~~~~~~~~~ test.cpp:20:10: note: Returned allocated memory return std::make_unique(bar); ^~~~~~ test.cpp:20:3: note: Potential leak of memory pointed to by field '_Myval2' return std::make_unique(bar); ^ 1 warning generated.

haoNoQ commented 5 years ago

I cannot reproduce it - neither on current clang, nor on r337125. This probably has something to do with libstdc++ vs. libc++. Could you attach a preprocessed file?

llvmbot commented 5 years ago

Seems to be resolved in Clang 8.0.

svenpanne commented 5 years ago

FYI: Exactly the same thing happens for me with the clang++-7 shipped with Ubuntu Cosmic. This is rather unfortunate, because it effectively makes the very helpful clang-analyzer-cplusplus.NewDeleteLeaks unusable for our project. Suppressing the warning where it happens is not really an option due to the amount of warnings. :-(

0x8000-0000 commented 6 years ago

assigned to @devincoughlin

pesitec commented 2 years ago

I reproduced the issue with clang-tidy 12.0.1 using much simpler code:

#include <memory>
std::unique_ptr<int> intMaker() {
    return std::make_unique<int>();
}

The false-positive only appears if C++17 is configured for the clang compiler, not if C++14 is configured.