Issue 143807
Summary False positive in clang-analyzer-cplusplus.NewDeleteLeaks for std::unique_ptr
Labels new issue
Assignees
Reporter ns-osmolsky
    ```
#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 
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to