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: dcough...@apple.com
          Reporter: hiradi...@msn.com
                CC: dcough...@apple.com, llvm-bugs@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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to