Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

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

Open Quuxplusone opened 6 years ago

Quuxplusone commented 6 years ago
Bugzilla Link PR38176
Status NEW
Importance P normal
Reported by Florin Iucha (florin@signbit.net)
Reported on 2018-07-16 04:10:29 -0700
Last modified on 2021-11-14 09:10:23 -0800
Version trunk
Hardware PC Linux
CC adam.balogh@ericsson.com, danielbaczynski8@gmail.com, george.burgess.iv@gmail.com, ken.redergard@nordicsemi.no, llvm-bugs@lists.llvm.org, noqnoqneo@gmail.com, pirama@google.com, srhines@google.com, svenpanne@gmail.com
Fixed by commit(s)
Attachments pp.zip (130021 bytes, application/x-zip-compressed)
Blocks
Blocked by
See also
florin@helios$ cat unique.cc
#include <cstdint>
#include <iostream>
#include <memory>

using Bar = uint64_t;

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

   const Bar& getBar()
   {
      return bar;
   }

private:
   Bar bar;
};

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

   return std::make_unique<Foo>(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<Foo>(bar);
   ^
unique.cc:27:11: note: Calling 'make_unique<Foo, unsigned long &>'
   return std::make_unique<Foo>(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<Foo>(bar);
          ^~~~~~~~~~~~~~~~~~~~~~~~~~
unique.cc:27:4: note: Potential leak of memory pointed to by field
'_M_head_impl'
   return std::make_unique<Foo>(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
Quuxplusone 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. :-(

Quuxplusone commented 5 years ago

Seems to be resolved in Clang 8.0.

Quuxplusone 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?

Quuxplusone 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<Foo>(bar);
  ^
test.cpp:20:10: note: Calling 'make_unique<Foo, unsigned long long &, 0>'
  return std::make_unique<Foo>(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<Foo>(bar);
         ^~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:20:3: note: Potential leak of memory pointed to by field '_Myval2'
  return std::make_unique<Foo>(bar);
  ^
1 warning generated.
Quuxplusone commented 4 years ago

Ken, could you please attach a preprocessed file?

Quuxplusone commented 4 years ago

Attached pp.zip (130021 bytes, application/x-zip-compressed): Preprocessed file, made with clang++ --analyze -Xanalyzer -analyzer-output=text -std=c++17 -E test.cpp

Quuxplusone commented 3 years ago

Any progress?

Quuxplusone commented 2 years ago

I think we can close this issue: I have just tried clang++-11.0.1 and clang++-12.0.0 with the OP's program on Ubuntu Hirsute (21.04), and both work as expected without any warnings.

No idea what fixed that, either some change in Clang since clang-7 or some change in libstdc++, but who cares. ;-)