[llvm-bugs] [Bug 42573] New: Memory leak checker doesn't report leak when constructor dereferences the argument

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jul 10 20:13:07 PDT 2019


https://bugs.llvm.org/show_bug.cgi?id=42573

            Bug ID: 42573
           Summary: Memory leak checker doesn't report leak when
                    constructor dereferences the argument
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Static Analyzer
          Assignee: dcoughlin at apple.com
          Reporter: hiraditya at msn.com
                CC: dcoughlin at apple.com, llvm-bugs at lists.llvm.org

Not sure why static analyzer doesn't report memory leak in the following case:

$ cat test.cpp
#include<memory>
#include<iostream>

struct A {
  A(const std::shared_ptr<int> &s) {
    std::cout << "constructor" << *s;
  }
};


int main() {
  auto l = std::make_shared<A>(nullptr);
  int *p = new int;
  int i = *p;
  return i;
}

However, if we remove the dereference of `s` it works.
$ cat test-no-deref-s.cpp

#include<memory>
#include<iostream>

struct A {
  A(const std::shared_ptr<int> &s) {
    std::cout << "constructor"; // removed the deref of s
  }
};


int main() {
  auto l = std::make_shared<A>(nullptr);
  int *p = new int;
  int i = *p;
  return i;
}

$ clang++ --analyze test-no-deref-s.cpp -std=c++14

test1.cpp:14:3: warning: Potential leak of memory pointed to by 'p'
  int i = *p;
  ^~~~~
1 warning generated.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190711/686eed6b/attachment.html>


More information about the llvm-bugs mailing list