llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.83k stars 11.46k forks source link

Analyzer misses memory leak if memory is allocated as default initialization of class member #26256

Open llvmbot opened 8 years ago

llvmbot commented 8 years ago
Bugzilla Link 25882
Version trunk
OS Linux
Attachments Code demonstrating the bug
Reporter LLVM Bugzilla Contributor

Extended Description

The example code defines two classes. A allocates memory as default initializer for a class member. B allocates the memory in the default constructor. Neither of the classes deletes those allocations. The Address Sanitizer reports both leaks.

$ g++ -std=c++14 -Wall -Wextra -fsanitize=address file1.cpp $ ./a.out

================================================================= ==21229==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 40 byte(s) in 1 object(s) allocated from:

​0 0x7f59d54a2a4a in operator new[](unsigned long) ../../../../gcc-5.3.0/libsanitizer/asan/asan_new_delete.cc:62

#​1 0x4009e5 in A::A() (a.out+0x4009e5)
#​2 0x400904 in main (a.out+0x400904)
#​3 0x7f59d47d2b04 in __libc_start_main (/lib64/libc.so.6+0x21b04)

Direct leak of 40 byte(s) in 1 object(s) allocated from:

​0 0x7f59d54a2a4a in operator new[](unsigned long) ../../../../gcc-5.3.0/libsanitizer/asan/asan_new_delete.cc:62

#​1 0x4009a1 in B::B() (a.out+0x4009a1)
#​2 0x400910 in main (a.out+0x400910)
#​3 0x7f59d47d2b04 in __libc_start_main (/lib64/libc.so.6+0x21b04)

SUMMARY: AddressSanitizer: 80 byte(s) leaked in 2 allocation(s).

The clang static analyzer detects only the leak of the allocation from B's default constructor. $ scan-build g++ -std=c++14 -Wall -Wextra -fsanitize=address file1.cpp scan-build: Using 'local/tmp/clang/bin/clang' for static analysis file1.cpp:18:1: warning: Potential leak of memory pointed to by 'b.p' } ^ 1 warning generated. scan-build: 1 bug found. scan-build: Run 'scan-view /tmp/scan-build-2015-12-18-153709-21294-1' to examine bug reports.

llvmbot commented 8 years ago

assigned to @tkremenek