Skip to content

Commit 328d2d9

Browse files
committed
[analyzer] Teach the analyzer that pointers can escape into __cxa_demangle
This fixes a reported false positive in the malloc checker. Differential Revision: https://reviews.llvm.org/D27599 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@289886 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 9f32816 commit 328d2d9

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

lib/StaticAnalyzer/Core/CallEvent.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ bool AnyFunctionCall::argumentsMayEscape() const {
382382
if (II->isStr("funopen"))
383383
return true;
384384

385+
// - __cxa_demangle - can reallocate memory and can return the pointer to
386+
// the input buffer.
387+
if (II->isStr("__cxa_demangle"))
388+
return true;
389+
385390
StringRef FName = II->getName();
386391

387392
// - CoreFoundation functions that end with "NoCopy" can free a passed-in

test/Analysis/Inputs/system-header-simulator-cxx.h

+9
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,12 @@ void* operator new (std::size_t size, void* ptr) throw() { return ptr; };
240240
void* operator new[] (std::size_t size, void* ptr) throw() { return ptr; };
241241
void operator delete (void* ptr, void*) throw() {};
242242
void operator delete[] (void* ptr, void*) throw() {};
243+
244+
namespace __cxxabiv1 {
245+
extern "C" {
246+
extern char *__cxa_demangle(const char *mangled_name,
247+
char *output_buffer,
248+
size_t *length,
249+
int *status);
250+
}}
251+
namespace abi = __cxxabiv1;

test/Analysis/malloc.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// RUN: %clang_cc1 -w -analyze -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -verify %s
22
// RUN: %clang_cc1 -triple i386-unknown-linux-gnu -w -analyze -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -verify %s
33

4+
#include "Inputs/system-header-simulator-cxx.h"
5+
46
typedef __typeof(sizeof(int)) size_t;
57
void *malloc(size_t);
68
void free(void *);
@@ -125,3 +127,15 @@ namespace PR31226 {
125127
p->m(); // no-crash // no-warning
126128
}
127129
}
130+
131+
// Allow __cxa_demangle to escape.
132+
char* test_cxa_demangle(const char* sym) {
133+
size_t funcnamesize = 256;
134+
char* funcname = (char*)malloc(funcnamesize);
135+
int status;
136+
char* ret = abi::__cxa_demangle(sym, funcname, &funcnamesize, &status);
137+
if (status == 0) {
138+
funcname = ret;
139+
}
140+
return funcname; // no-warning
141+
}

0 commit comments

Comments
 (0)