Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,alpha.cplusplus.NewDelete -std=c++11 -verify %s |
| 2 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,alpha.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)); |
| 19 | } // expected-warning{{Memory is never released; potential leak of memory pointed to by 'p'}} |
| 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 |
| 58 | // expected-warning@-2 {{Memory is never released; potential leak of memory pointed to by 'p'}} |
| 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 | } |