Jordan Rose | 10ad081 | 2013-04-05 17:55:07 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete -std=c++11 -verify %s |
| 2 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete,alpha.cplusplus.NewDeleteLeaks -DLEAKS -std=c++11 -verify %s |
Anton Yartsev | e3377fb | 2013-04-04 23:46:29 +0000 | [diff] [blame] | 3 | |
| 4 | typedef __typeof(sizeof(int)) size_t; |
| 5 | void *malloc(size_t); |
| 6 | void free(void *); |
| 7 | |
| 8 | //-------------------------------------------------- |
| 9 | // Check that unix.Malloc catches all types of bugs. |
| 10 | //-------------------------------------------------- |
| 11 | void testMallocDoubleFree() { |
| 12 | int *p = (int *)malloc(sizeof(int)); |
| 13 | free(p); |
| 14 | free(p); // expected-warning{{Attempt to free released memory}} |
| 15 | } |
| 16 | |
| 17 | void testMallocLeak() { |
| 18 | int *p = (int *)malloc(sizeof(int)); |
Anna Zaks | a1de856 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 19 | } // expected-warning{{Potential leak of memory pointed to by 'p'}} |
Anton Yartsev | e3377fb | 2013-04-04 23:46:29 +0000 | [diff] [blame] | 20 | |
| 21 | void testMallocUseAfterFree() { |
| 22 | int *p = (int *)malloc(sizeof(int)); |
| 23 | free(p); |
| 24 | int j = *p; // expected-warning{{Use of memory after it is freed}} |
| 25 | } |
| 26 | |
| 27 | void testMallocBadFree() { |
| 28 | int i; |
| 29 | free(&i); // expected-warning{{Argument to free() is the address of the local variable 'i', which is not memory allocated by malloc()}} |
| 30 | } |
| 31 | |
| 32 | void testMallocOffsetFree() { |
| 33 | int *p = (int *)malloc(sizeof(int)); |
| 34 | free(++p); // expected-warning{{Argument to free() is offset by 4 bytes from the start of memory allocated by malloc()}} |
| 35 | } |
| 36 | |
| 37 | //----------------------------------------------------------------- |
| 38 | // Check that unix.MismatchedDeallocator catches all types of bugs. |
| 39 | //----------------------------------------------------------------- |
| 40 | void testMismatchedDeallocator() { |
| 41 | int *x = (int *)malloc(sizeof(int)); |
| 42 | delete x; // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not 'delete'}} |
| 43 | } |
| 44 | |
| 45 | //---------------------------------------------------------------- |
| 46 | // Check that alpha.cplusplus.NewDelete catches all types of bugs. |
| 47 | //---------------------------------------------------------------- |
| 48 | void testNewDoubleFree() { |
| 49 | int *p = new int; |
| 50 | delete p; |
| 51 | delete p; // expected-warning{{Attempt to free released memory}} |
| 52 | } |
| 53 | |
| 54 | void testNewLeak() { |
| 55 | int *p = new int; |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 56 | } |
| 57 | #ifdef LEAKS |
Anna Zaks | a1de856 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 58 | // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}} |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 59 | #endif |
Anton Yartsev | e3377fb | 2013-04-04 23:46:29 +0000 | [diff] [blame] | 60 | |
| 61 | void testNewUseAfterFree() { |
| 62 | int *p = (int *)operator new(0); |
| 63 | delete p; |
| 64 | int j = *p; // expected-warning{{Use of memory after it is freed}} |
| 65 | } |
| 66 | |
| 67 | void testNewBadFree() { |
| 68 | int i; |
| 69 | delete &i; // expected-warning{{Argument to 'delete' is the address of the local variable 'i', which is not memory allocated by 'new'}} |
| 70 | } |
| 71 | |
| 72 | void testNewOffsetFree() { |
| 73 | int *p = new int; |
| 74 | operator delete(++p); // expected-warning{{Argument to operator delete is offset by 4 bytes from the start of memory allocated by 'new'}} |
| 75 | } |
Anna Zaks | 93a21a8 | 2013-04-09 00:30:28 +0000 | [diff] [blame^] | 76 | |
| 77 | //---------------------------------------------------------------- |
| 78 | // Test that we check for free errors on escaped pointers. |
| 79 | //---------------------------------------------------------------- |
| 80 | void changePtr(int **p); |
| 81 | static int *globalPtr; |
| 82 | void changePointee(int *p); |
| 83 | |
| 84 | void testMismatchedChangePtrThroughCall() { |
| 85 | int *p = (int*)malloc(sizeof(int)*4); |
| 86 | changePtr(&p); |
| 87 | delete p; // no-warning the value of the pointer might have changed |
| 88 | } |
| 89 | |
| 90 | void testMismatchedChangePointeeThroughCall() { |
| 91 | int *p = (int*)malloc(sizeof(int)*4); |
| 92 | changePointee(p); |
| 93 | delete p; // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not 'delete'}} |
| 94 | } |
| 95 | |
| 96 | void testShouldReportDoubleFreeNotMismatched() { |
| 97 | int *p = (int*)malloc(sizeof(int)*4); |
| 98 | globalPtr = p; |
| 99 | free(p); |
| 100 | delete globalPtr; // expected-warning {{Attempt to free released memory}} |
| 101 | } |
| 102 | |
| 103 | void testMismatchedChangePointeeThroughAssignment() { |
| 104 | int *arr = new int[4]; |
| 105 | globalPtr = arr; |
| 106 | delete arr; // expected-warning{{Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'delete'}} |
| 107 | } |