diff options
author | Reka Kovacs <rekanikolett@gmail.com> | 2018-06-09 13:03:49 +0000 |
---|---|---|
committer | Reka Kovacs <rekanikolett@gmail.com> | 2018-06-09 13:03:49 +0000 |
commit | 18775fc9b7d196d8f5aa9540417d684f5eed74ff (patch) | |
tree | 19887ced617c592f199c92aa8fe029dcf405d6f5 /clang/test/Analysis/dangling-internal-buffer.cpp | |
parent | 8aada65f8131abdc4b72ae33fd57fcb35f1fe5b3 (diff) | |
download | bcm5719-llvm-18775fc9b7d196d8f5aa9540417d684f5eed74ff.tar.gz bcm5719-llvm-18775fc9b7d196d8f5aa9540417d684f5eed74ff.zip |
[analyzer] Add dangling internal buffer check.
This check will mark raw pointers to C++ standard library container internal
buffers 'released' when the objects themselves are destroyed. Such information
can be used by MallocChecker to warn about use-after-free problems.
In this first version, 'std::basic_string's are supported.
Differential Revision: https://reviews.llvm.org/D47135
llvm-svn: 334348
Diffstat (limited to 'clang/test/Analysis/dangling-internal-buffer.cpp')
-rw-r--r-- | clang/test/Analysis/dangling-internal-buffer.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/clang/test/Analysis/dangling-internal-buffer.cpp b/clang/test/Analysis/dangling-internal-buffer.cpp new file mode 100644 index 00000000000..791bb960477 --- /dev/null +++ b/clang/test/Analysis/dangling-internal-buffer.cpp @@ -0,0 +1,71 @@ +//RUN: %clang_analyze_cc1 -analyzer-checker=alpha.cplusplus.DanglingInternalBuffer %s -analyzer-output=text -verify + +namespace std { + +template< typename CharT > +class basic_string { +public: + ~basic_string(); + const CharT *c_str(); +}; + +typedef basic_string<char> string; +typedef basic_string<wchar_t> wstring; +typedef basic_string<char16_t> u16string; +typedef basic_string<char32_t> u32string; + +} // end namespace std + +void consume(const char *) {} +void consume(const wchar_t *) {} +void consume(const char16_t *) {} +void consume(const char32_t *) {} + +void deref_after_scope_char() { + const char *c; + { + std::string s; + c = s.c_str(); + } + consume(c); // expected-warning {{Use of memory after it is freed}} + // expected-note@-1 {{Use of memory after it is freed}} +} + +void deref_after_scope_wchar_t() { + const wchar_t *w; + { + std::wstring ws; + w = ws.c_str(); + } + consume(w); // expected-warning {{Use of memory after it is freed}} + // expected-note@-1 {{Use of memory after it is freed}} +} + +void deref_after_scope_char16_t() { + const char16_t *c16; + { + std::u16string s16; + c16 = s16.c_str(); + } + consume(c16); // expected-warning {{Use of memory after it is freed}} + // expected-note@-1 {{Use of memory after it is freed}} +} + +void deref_after_scope_char32_t() { + const char32_t *c32; + { + std::u32string s32; + c32 = s32.c_str(); + } + consume(c32); // expected-warning {{Use of memory after it is freed}} + // expected-note@-1 {{Use of memory after it is freed}} +} + +void deref_after_scope_ok() { + const char *c; + std::string s; + { + c = s.c_str(); + } + consume(c); // no-warning +} |