<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/143807>143807</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
False positive in clang-analyzer-cplusplus.NewDeleteLeaks for std::unique_ptr
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ns-osmolsky
</td>
</tr>
</table>
<pre>
```
#include <memory>
#include <cstring>
#include <string>
struct Repro {
std::unique_ptr<int> m_ptr;
struct Other {
char url[256];
};
char m_buff[1000];
void CopyIn()
{
auto other = (Other *)m_buff;
std::string local_url = "foo bar";
strncpy(other->url, local_url.c_str(), sizeof(other->url) - 1);
}
};
void
bug()
{
Repro rep;
rep.m_ptr = std::make_unique<int>();
rep.CopyIn();
}
```
`clang-tidy` reports the following when building against the GCC-15's libstdc++:
```
repro.cpp:19:5: error: Potential leak of memory pointed to by field '_M_head_impl' [clang-analyzer-cplusplus.NewDeleteLeaks,-warnings-as-errors]
19 | }
| ^
repro.cpp:26:17: note: Calling 'make_unique<int, >'
26 | rep.m_ptr = std::make_unique<int>();
| ^~~~~~~~~~~~~~~~~~~~~~~
/opt/gcc-15/lib/gcc/x86_64-pc-linux-gnu/15.1.1/../../../../include/c++/15.1.1/bits/unique_ptr.h:1085:30: note: Memory is allocated
1085 | { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repro.cpp:26:17: note: Returned allocated memory
26 | rep.m_ptr = std::make_unique<int>();
| ^~~~~~~~~~~~~~~~~~~~~~~
repro.cpp:27:5: note: Calling 'Repro::CopyIn'
27 | rep.CopyIn();
| ^~~~~~~~~~~~
repro.cpp:19:5: note: Potential leak of memory pointed to by field '_M_head_impl'
19 | }
| ^
```
There is clearly no leak here and the warning goes away if the code is tweaked just a little bit... I cannot understand
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8Vk2PozgT_jXOpQQCE74OHEi682qkd3ZXo7kjAwXxtGOztulM5rC_fWUgX5O0dqWRNko3OK4qP_VU1QPMGN5LxILEGxK_rNho90oX0njKHJQwb6dVrdpTQZJg-QYloRGXjRhbBBJtD3hQ-kSi14edxljNZf9s624nKI3VY2PhCw5aAUk3JCgBAIxtSVSSqBwl_3PEarCaRFsuLYle4TAvN3OE2XyK8rvdo76J4j7NnmkYtSDxhsYJiV9mR7dF0pvFZHeo6rHrSLwJgyC42C4W74q3sFXD6ZMkNCM0v4S5PY-NVoGagUQvQGi2oKIloflywG3Yu3RndkCoholq1GKJQTuloGaaULogNlbLZjgRmk1neSR6dUnS7dXXbypj9QKVbsHwH6i6B48cPAidyRzYkRKUF2pIULq8SVDWY39J-5ryXDiNw5VJjYM_lWgCf0ntwN6wmst5KeUS8M71juEZwwzptg-nZSOY7D3L2xNJAuertDVg9widEkIdHZPHPUqoRy5at2I949LYyeZ_260XxoSmBgSvjW0bQjfuG5WXEy4Hapem3wwDicowJ1EZk6gE1Fppd_OHsigtZwIEsjdQHcyzAYPi0mILVkF9go6jaIHQtPpc7ZG1FT8MgtAUSLyZk2GSidMP1F4ziNG4P_83PL6gQIv_R_ZmCN16R6Yll73xmPEmCMa16sRgmANJt0sR595y6-kav_6UCE1cNqlLQCqL7rplQjiiCE0f60W3MNUsnWPT5BL7F0o-AyTx619PP5N-7NRgCd31TTNVbCd4PS8J3X3PkipZe0PjCS7H714vR0J3YeyHfkjozvcf_y1qROjuXPIbh5pbQ-juqjv-3rEUZK7iUXBL1ue5xtwAE27qLLo5AWd7ZT3dgEY7agl3UlZ9HWYyJB7BLWh2oa1T-sh066xK3ZvZrqqYu6e573LIZw4fCv0hj094_YdW-DKhxvaa3NLU_23171Cm58l70q-TEs0nnyXk3KjpHdRHgfkIyYeDfz7-F-f-bmbPT6Tnc_uT-n3do0bXeY1ApsUJpJohTL8z2U4StwgF9AoNsC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