Dominic Chen | 184c624 | 2017-03-03 18:02:02 +0000 | [diff] [blame] | 1 | // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -fblocks -verify %s |
| 2 | // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete,cplusplus.NewDeleteLeaks -std=c++11 -DLEAKS -fblocks -verify %s |
Artem Dergachev | d3c5431 | 2018-01-24 21:24:10 +0000 | [diff] [blame] | 3 | // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -fblocks -DTEST_INLINABLE_ALLOCATORS -verify %s |
| 4 | // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete,cplusplus.NewDeleteLeaks -std=c++11 -DLEAKS -fblocks -DTEST_INLINABLE_ALLOCATORS -verify %s |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 5 | #include "Inputs/system-header-simulator-cxx.h" |
| 6 | #include "Inputs/system-header-simulator-objc.h" |
| 7 | |
| 8 | typedef __typeof__(sizeof(int)) size_t; |
| 9 | extern "C" void *malloc(size_t); |
Anton Yartsev | 5b5c7ce | 2015-02-19 13:36:20 +0000 | [diff] [blame] | 10 | extern "C" void *alloca(size_t); |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 11 | extern "C" void free(void *); |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 12 | |
| 13 | //---------------------------------------------------------------------------- |
| 14 | // Check for intersections with unix.Malloc and unix.MallocWithAnnotations |
| 15 | // checkers bounded with cplusplus.NewDelete. |
| 16 | //---------------------------------------------------------------------------- |
| 17 | |
Anton Yartsev | 06dc8aa | 2013-04-05 00:37:32 +0000 | [diff] [blame] | 18 | //----- malloc()/free() are subjects of unix.Malloc and unix.MallocWithAnnotations |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 19 | void testMallocFreeNoWarn() { |
| 20 | int i; |
| 21 | free(&i); // no warn |
| 22 | |
| 23 | int *p1 = (int *)malloc(sizeof(int)); |
| 24 | free(++p1); // no warn |
| 25 | |
| 26 | int *p2 = (int *)malloc(sizeof(int)); |
| 27 | free(p2); |
| 28 | free(p2); // no warn |
| 29 | |
| 30 | int *p3 = (int *)malloc(sizeof(int)); // no warn |
Anton Yartsev | 06dc8aa | 2013-04-05 00:37:32 +0000 | [diff] [blame] | 31 | |
| 32 | int *p4 = (int *)malloc(sizeof(int)); |
| 33 | free(p4); |
| 34 | int j = *p4; // no warn |
Anton Yartsev | 5b5c7ce | 2015-02-19 13:36:20 +0000 | [diff] [blame] | 35 | |
| 36 | int *p5 = (int *)alloca(sizeof(int)); |
| 37 | free(p5); // no warn |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Anton Yartsev | ae3630b | 2013-03-30 01:22:45 +0000 | [diff] [blame] | 40 | void testDeleteMalloced() { |
Anton Yartsev | 5b5c7ce | 2015-02-19 13:36:20 +0000 | [diff] [blame] | 41 | int *p1 = (int *)malloc(sizeof(int)); |
| 42 | delete p1; // no warn |
| 43 | |
| 44 | int *p2 = (int *)__builtin_alloca(sizeof(int)); |
| 45 | delete p2; // no warn |
Anton Yartsev | ae3630b | 2013-03-30 01:22:45 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Anton Yartsev | b50f4ba | 2015-04-14 14:18:04 +0000 | [diff] [blame] | 48 | void testUseZeroAllocatedMalloced() { |
| 49 | int *p1 = (int *)malloc(0); |
| 50 | *p1 = 1; // no warn |
| 51 | } |
| 52 | |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 53 | //----- Test free standard new |
| 54 | void testFreeOpNew() { |
| 55 | void *p = operator new(0); |
| 56 | free(p); |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 57 | } |
| 58 | #ifdef LEAKS |
Anna Zaks | a1de856 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 59 | // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}} |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 60 | #endif |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 61 | |
| 62 | void testFreeNewExpr() { |
| 63 | int *p = new int; |
| 64 | free(p); |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 65 | } |
| 66 | #ifdef LEAKS |
Anna Zaks | a1de856 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 67 | // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}} |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 68 | #endif |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 69 | |
| 70 | void testObjcFreeNewed() { |
| 71 | int *p = new int; |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 72 | NSData *nsdata = [NSData dataWithBytesNoCopy:p length:sizeof(int) freeWhenDone:1]; |
| 73 | #ifdef LEAKS |
Anna Zaks | a1de856 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 74 | // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}} |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 75 | #endif |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 76 | } |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 77 | |
| 78 | void testFreeAfterDelete() { |
| 79 | int *p = new int; |
| 80 | delete p; |
| 81 | free(p); // expected-warning{{Use of memory after it is freed}} |
| 82 | } |
| 83 | |
| 84 | void testStandardPlacementNewAfterDelete() { |
| 85 | int *p = new int; |
| 86 | delete p; |
| 87 | p = new(p) int; // expected-warning{{Use of memory after it is freed}} |
| 88 | } |