Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] False positive diagnostic for NewDeleteLeaks #122405

Open
ChuanqiXu9 opened this issue Jan 10, 2025 · 4 comments
Open

[clang-tidy] False positive diagnostic for NewDeleteLeaks #122405

ChuanqiXu9 opened this issue Jan 10, 2025 · 4 comments
Labels
clang:static analyzer false-positive Warning fires when it should not incomplete Issue not complete (e.g. missing a reproducer, build arguments, etc.)

Comments

@ChuanqiXu9
Copy link
Member

Reproducer:

#include <vector>
#include <memory>

void RuleParseCommon() { std::vector{std::make_shared<int>()}; }

Reproducer reduced by cvise:

namespace std {
template <class E> struct initializer_list {
  const E *begin;
  const E *end;
};
template <typename _Tp> struct vector {
  vector(initializer_list<_Tp>);
};
struct _Sp_counted_base {};
struct __shared_count {
  __shared_count(int, int) {
    auto __pi = new _Sp_counted_base;
    _M_pi = __pi;
  }
  ~__shared_count();
  _Sp_counted_base *_M_pi;
};
int _M_ptr;
struct __shared_ptr {
  template <typename _Alloc>
  __shared_ptr(_Alloc __tag) : _M_refcount(_M_ptr, __tag) {}
  __shared_count _M_refcount;
};
struct shared_ptr : __shared_ptr {
  shared_ptr(int __tag) : __shared_ptr(__tag) {}
};
int allocate_shared___a;
shared_ptr allocate_shared() { return allocate_shared___a; }
shared_ptr make_shared() { return allocate_shared(); }
} // namespace std
using std::vector;

void RuleParseCommon() { vector{std::make_shared()}; }

Command:

clang-tidy test.cc  -checks='clang-analyzer-cplusplus.NewDeleteLeaks' -- -std=gnu++20

we got:

Potential leak of memory pointed to by field '_M_pi' [clang-analyzer-cplusplus.NewDeleteLeaks]
@ChuanqiXu9 ChuanqiXu9 added clang-tidy false-positive Warning fires when it should not labels Jan 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 10, 2025

@llvm/issue-subscribers-clang-tidy

Author: Chuanqi Xu (ChuanqiXu9)

Reproducer:
#include &lt;vector&gt;
#include &lt;memory&gt;

void RuleParseCommon() { std::vector{std::make_shared&lt;int&gt;()}; }

Reproducer reduced by cvise:

namespace std {
template &lt;class E&gt; struct initializer_list {
  const E *begin;
  const E *end;
};
template &lt;typename _Tp&gt; struct vector {
  vector(initializer_list&lt;_Tp&gt;);
};
struct _Sp_counted_base {};
struct __shared_count {
  __shared_count(int, int) {
    auto __pi = new _Sp_counted_base;
    _M_pi = __pi;
  }
  ~__shared_count();
  _Sp_counted_base *_M_pi;
};
int _M_ptr;
struct __shared_ptr {
  template &lt;typename _Alloc&gt;
  __shared_ptr(_Alloc __tag) : _M_refcount(_M_ptr, __tag) {}
  __shared_count _M_refcount;
};
struct shared_ptr : __shared_ptr {
  shared_ptr(int __tag) : __shared_ptr(__tag) {}
};
int allocate_shared___a;
shared_ptr allocate_shared() { return allocate_shared___a; }
shared_ptr make_shared() { return allocate_shared(); }
} // namespace std
using std::vector;

void RuleParseCommon() { vector{std::make_shared()}; }

Command:

clang-tidy test.cc  -checks='clang-analyzer-cplusplus.NewDeleteLeaks' -- -std=gnu++20

we got:

Potential leak of memory pointed to by field '_M_pi' [clang-analyzer-cplusplus.NewDeleteLeaks]

@llvmbot
Copy link
Member

llvmbot commented Jan 10, 2025

@llvm/issue-subscribers-clang-static-analyzer

Author: Chuanqi Xu (ChuanqiXu9)

Reproducer:
#include &lt;vector&gt;
#include &lt;memory&gt;

void RuleParseCommon() { std::vector{std::make_shared&lt;int&gt;()}; }

Reproducer reduced by cvise:

namespace std {
template &lt;class E&gt; struct initializer_list {
  const E *begin;
  const E *end;
};
template &lt;typename _Tp&gt; struct vector {
  vector(initializer_list&lt;_Tp&gt;);
};
struct _Sp_counted_base {};
struct __shared_count {
  __shared_count(int, int) {
    auto __pi = new _Sp_counted_base;
    _M_pi = __pi;
  }
  ~__shared_count();
  _Sp_counted_base *_M_pi;
};
int _M_ptr;
struct __shared_ptr {
  template &lt;typename _Alloc&gt;
  __shared_ptr(_Alloc __tag) : _M_refcount(_M_ptr, __tag) {}
  __shared_count _M_refcount;
};
struct shared_ptr : __shared_ptr {
  shared_ptr(int __tag) : __shared_ptr(__tag) {}
};
int allocate_shared___a;
shared_ptr allocate_shared() { return allocate_shared___a; }
shared_ptr make_shared() { return allocate_shared(); }
} // namespace std
using std::vector;

void RuleParseCommon() { vector{std::make_shared()}; }

Command:

clang-tidy test.cc  -checks='clang-analyzer-cplusplus.NewDeleteLeaks' -- -std=gnu++20

we got:

Potential leak of memory pointed to by field '_M_pi' [clang-analyzer-cplusplus.NewDeleteLeaks]

@steakhal
Copy link
Contributor

I could not reproduce the issue: https://compiler-explorer.com/z/oMWdPW66a
What clang-tidy version do you use?
Could you reproduce the issue on Compiler Explorer?

@steakhal steakhal added the incomplete Issue not complete (e.g. missing a reproducer, build arguments, etc.) label Jan 10, 2025
@steakhal
Copy link
Contributor

FYI I had a look at the preprocessed reduced version, and that of course reproduces the issue. However, without seeing the original case it's hard to tell if it's because the reduction actually implanted the leak due to the cuts it made or because it originally had the leak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer false-positive Warning fires when it should not incomplete Issue not complete (e.g. missing a reproducer, build arguments, etc.)
Projects
None yet
Development

No branches or pull requests

4 participants