diff options
author | Jordan Rose <jordan_rose@apple.com> | 2013-03-20 20:35:57 +0000 |
---|---|---|
committer | Jordan Rose <jordan_rose@apple.com> | 2013-03-20 20:35:57 +0000 |
commit | 5d22fcb257176bcc8823b1cddf3104737e6a237f (patch) | |
tree | 224dd08645dd3897794f3dcb1a66b2f2fc7f330f /clang/test/Analysis/malloc.c | |
parent | 5413aaa791c2c35eca1ea041899e1666e8d46602 (diff) | |
download | bcm5719-llvm-5d22fcb257176bcc8823b1cddf3104737e6a237f.tar.gz bcm5719-llvm-5d22fcb257176bcc8823b1cddf3104737e6a237f.zip |
[analyzer] Track malloc'd memory into struct fields.
Due to improper modelling of copy constructors (specifically, their
const reference arguments), we were producing spurious leak warnings
for allocated memory stored in structs. In order to silence this, we
decided to consider storing into a struct to be the same as escaping.
However, the previous commit has fixed this issue and we can now properly
distinguish leaked memory that happens to be in a struct from a buffer
that escapes within a struct wrapper.
Originally applied in r161511, reverted in r174468.
<rdar://problem/12945937>
llvm-svn: 177571
Diffstat (limited to 'clang/test/Analysis/malloc.c')
-rw-r--r-- | clang/test/Analysis/malloc.c | 66 |
1 files changed, 32 insertions, 34 deletions
diff --git a/clang/test/Analysis/malloc.c b/clang/test/Analysis/malloc.c index e8b4ad3b4df..7790b32b078 100644 --- a/clang/test/Analysis/malloc.c +++ b/clang/test/Analysis/malloc.c @@ -531,6 +531,12 @@ int *testMalloc3() { return y; // no-warning } +void testStructLeak() { + StructWithPtr St; + St.memP = malloc(12); + return; // expected-warning {{Memory is never released; potential leak of memory pointed to by 'St.memP'}} +} + void testElemRegion1() { char *x = (void*)malloc(2); int *ix = (int*)x; @@ -929,6 +935,18 @@ int cmpHeapAllocationToUnknown() { return 0; } +void localArrayTest() { + char *p = (char*)malloc(12); + char *ArrayL[12]; + ArrayL[0] = p; +} // expected-warning {{leak}} + +void localStructTest() { + StructWithPtr St; + StructWithPtr *pSt = &St; + pSt->memP = malloc(12); +} // expected-warning{{Memory is never released; potential leak}} + #ifdef __INTPTR_TYPE__ // Test double assignment through integers. typedef __INTPTR_TYPE__ intptr_t; @@ -1045,50 +1063,18 @@ void testPassToSystemHeaderFunctionIndirectly() { fakeSystemHeaderCallInt(p); } // expected-warning {{leak}} -// ---------------------------------------------------------------------------- -// False negatives. - -// TODO: This is another false negative. -void testMallocWithParam(int **p) { - *p = (int*) malloc(sizeof(int)); - *p = 0; -} - -void testMallocWithParam_2(int **p) { - *p = (int*) malloc(sizeof(int)); -} - -// Pending on removal of the escaping on assignment to struct fields. -void testStructLeak() { - StructWithPtr St; - St.memP = malloc(12); - return; // missing warning -} - -void localArrayTest() { - char *p = (char*)malloc(12); - char *ArrayL[12]; - ArrayL[0] = p; -} // missing warning - -void localStructTest() { - StructWithPtr St; - StructWithPtr *pSt = &St; - pSt->memP = malloc(12); -} // missing warning - void testPassConstPointerIndirectlyStruct() { struct HasPtr hp; hp.p = malloc(10); memcmp(&hp, &hp, sizeof(hp)); - return; // missing leak + return; // expected-warning {{Memory is never released; potential leak of memory pointed to by 'hp.p'}} } void testPassToSystemHeaderFunctionIndirectlyStruct() { SomeStruct ss; ss.p = malloc(1); fakeSystemHeaderCall(&ss); -} // missing leak +} // expected-warning {{Memory is never released; potential leak of memory pointed to by 'ss.p'}} int *testOffsetAllocate(size_t size) { int *memoryBlock = (int *)malloc(size + sizeof(int)); @@ -1202,3 +1188,15 @@ void freeMemory() { poolFreeC(_vectorSegments[_nVectorSegments++]); } } + +// ---------------------------------------------------------------------------- +// False negatives. + +void testMallocWithParam(int **p) { + *p = (int*) malloc(sizeof(int)); + *p = 0; // FIXME: should warn here +} + +void testMallocWithParam_2(int **p) { + *p = (int*) malloc(sizeof(int)); // no-warning +} |