Skip to content

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

Open
@ChuanqiXu9

Description

@ChuanqiXu9

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:static analyzerfalse-positiveWarning fires when it should notincompleteIssue not complete (e.g. missing a reproducer, build arguments, etc.)worksformeResolved as "works for me"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions