Open gumb0 opened 3 years ago
I tried with libc++, too, and can confirm that it's not reproduced with it.
Preprocessed example Here's the preprocessed file.
I'm also using libstdc++ locally.
Surprisingly, i can't reproduce this one. Godbolt uses libstdc++ which i don't have, and also godbolt doesn't seem to produce sane preprocessed files when fed the -E option (it filters a lot of stuff that makes sense to filter in normal compiler stdout). Regardless of whether i take my own libc++ or i try to correct errors in godbolt's preprocessed file until it compiles, i don't see the buggy warning. Andrei, do you happen to have a preprocessed file of your example?
assigned to @devincoughlin
This may be the simpler variant of it. https://godbolt.org/z/516YdMW4r
#include <memory>
std::unique_ptr<int> select(bool c)
{
return c ? std::make_unique<int>() : nullptr;
}
void test(bool c) { select(c); }
> clang-tidy-14 --checks='-*,clang-analyzer-cplusplus.NewDeleteLeaks' source.cpp -extra-arg=-std=c++17
source.cpp:8:21: error: Potential leak of memory pointed to by field '_M_head_impl' [clang-analyzer-cplusplus.NewDeleteLeaks,-warnings-as-errors]
void test(bool c) { select(c); }
^
source.cpp:8:21: note: Calling 'select'
void test(bool c) { select(c); }
^~~~~~~~~
source.cpp:5:12: note: Assuming 'c' is true
return c ? std::make_unique<int>() : nullptr;
^
source.cpp:5:12: note: '?' condition is true
source.cpp:5:16: note: Calling 'make_unique<int, >'
return c ? std::make_unique<int>() : nullptr;
^~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/unique_ptr.h:984:30: note: Memory is allocated
{ return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
source.cpp:5:16: note: Returned allocated memory
return c ? std::make_unique<int>() : nullptr;
^~~~~~~~~~~~~~~~~~~~~~~
source.cpp:8:21: note: Returned allocated memory
void test(bool c) { select(c); }
^~~~~~~~~
source.cpp:8:21: note: Potential leak of memory pointed to by field '_M_head_impl'
1 warning treated as error
Extended Description
For the following code the warning is generated about potential memory leak of unique_ptr's data returnd in a tuple and put into a struct, but also some warning about vector's data leaked, I believe both are false positives.
This is the most minimal code I was able to produce, the issues disappear when I delete anything inside fn().
https://godbolt.org/z/ceG3ce