summaryrefslogtreecommitdiffstats
path: root/clang/test/Analysis/malloc.cpp
diff options
context:
space:
mode:
authorArtem Dergachev <artem.dergachev@gmail.com>2019-04-13 02:01:45 +0000
committerArtem Dergachev <artem.dergachev@gmail.com>2019-04-13 02:01:45 +0000
commit7d4694547a6b6593fb537cd90893121f484e9b4d (patch)
tree795e52f7626ada7ea28c84f1d233776942678e24 /clang/test/Analysis/malloc.cpp
parent5e67abd91f406c62973fb38116270a391f1d7b42 (diff)
downloadbcm5719-llvm-7d4694547a6b6593fb537cd90893121f484e9b4d.tar.gz
bcm5719-llvm-7d4694547a6b6593fb537cd90893121f484e9b4d.zip
[analyzer] Escape pointers stored into top-level parameters with destructors.
Writing stuff into an argument variable is usually equivalent to writing stuff to a local variable: it will have no effect outside of the function. There's an important exception from this rule: if the argument variable has a non-trivial destructor, the destructor would be invoked on the parent stack frame, exposing contents of the otherwise dead argument variable to the caller. If such argument is the last place where a pointer is stored before the function exits and the function is the one we've started our analysis from (i.e., we have no caller context for it), we currently diagnose a leak. This is incorrect because the destructor of the argument still has access to the pointer. The destructor may deallocate the pointer or even pass it further. Treat writes into such argument regions as "escapes" instead, suppressing spurious memory leak reports but not messing with dead symbol removal. Differential Revision: https://reviews.llvm.org/D60112 llvm-svn: 358321
Diffstat (limited to 'clang/test/Analysis/malloc.cpp')
-rw-r--r--clang/test/Analysis/malloc.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/clang/test/Analysis/malloc.cpp b/clang/test/Analysis/malloc.cpp
index b93c73e591c..6e5a0e4d597 100644
--- a/clang/test/Analysis/malloc.cpp
+++ b/clang/test/Analysis/malloc.cpp
@@ -141,3 +141,26 @@ char* test_cxa_demangle(const char* sym) {
}
return funcname; // no-warning
}
+
+namespace argument_leak {
+class A {
+ char *name;
+
+public:
+ char *getName() {
+ if (!name) {
+ name = static_cast<char *>(malloc(10));
+ }
+ return name;
+ }
+ ~A() {
+ if (name) {
+ delete[] name;
+ }
+ }
+};
+
+void test(A a) {
+ (void)a.getName();
+}
+} // namespace argument_leak
OpenPOWER on IntegriCloud