Jordan Rose | 89e5aaf | 2012-07-16 23:38:09 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-store region -std=c++11 -verify %s |
Zhongxing Xu | 40ab43b | 2010-04-20 05:48:57 +0000 | [diff] [blame] | 2 | |
Jordan Rose | e38c1c2 | 2012-06-20 01:32:01 +0000 | [diff] [blame] | 3 | void clang_analyzer_eval(bool); |
| 4 | |
Jordan Rose | 89e5aaf | 2012-07-16 23:38:09 +0000 | [diff] [blame] | 5 | typedef __typeof__(sizeof(int)) size_t; |
Jordan Rose | e38c1c2 | 2012-06-20 01:32:01 +0000 | [diff] [blame] | 6 | extern "C" void *malloc(size_t); |
| 7 | |
Jordan Rose | ee68111 | 2012-06-25 20:48:28 +0000 | [diff] [blame] | 8 | int someGlobal; |
| 9 | void testImplicitlyDeclaredGlobalNew() { |
| 10 | if (someGlobal != 0) |
| 11 | return; |
| 12 | |
| 13 | // This used to crash because the global operator new is being implicitly |
| 14 | // declared and it does not have a valid source location. (PR13090) |
| 15 | void *x = ::operator new(0); |
| 16 | ::operator delete(x); |
| 17 | |
| 18 | // Check that the new/delete did not invalidate someGlobal; |
| 19 | clang_analyzer_eval(someGlobal == 0); // expected-warning{{TRUE}} |
| 20 | } |
| 21 | |
| 22 | |
Jordan Rose | e38c1c2 | 2012-06-20 01:32:01 +0000 | [diff] [blame] | 23 | // This is the standard placement new. |
| 24 | inline void* operator new(size_t, void* __p) throw() |
| 25 | { |
| 26 | return __p; |
Zhongxing Xu | 48fb322 | 2010-04-21 02:22:25 +0000 | [diff] [blame] | 27 | } |
Zhongxing Xu | 40ab43b | 2010-04-20 05:48:57 +0000 | [diff] [blame] | 28 | |
Jordan Rose | e38c1c2 | 2012-06-20 01:32:01 +0000 | [diff] [blame] | 29 | void *testPlacementNew() { |
| 30 | int *x = (int *)malloc(sizeof(int)); |
| 31 | *x = 1; |
| 32 | clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}}; |
| 33 | |
| 34 | void *y = new (x) int; |
| 35 | clang_analyzer_eval(x == y); // expected-warning{{TRUE}}; |
| 36 | clang_analyzer_eval(*x == 1); // expected-warning{{UNKNOWN}}; |
| 37 | |
| 38 | return y; |
| 39 | } |
| 40 | |
| 41 | void *operator new(size_t, size_t, int *); |
| 42 | void *testCustomNew() { |
| 43 | int x[1] = {1}; |
| 44 | clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}}; |
| 45 | |
| 46 | void *y = new (0, x) int; |
| 47 | clang_analyzer_eval(*x == 1); // expected-warning{{UNKNOWN}}; |
| 48 | |
| 49 | return y; // no-warning |
Zhongxing Xu | 40ab43b | 2010-04-20 05:48:57 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Jordan Rose | 70cbf3c | 2012-07-02 22:21:47 +0000 | [diff] [blame] | 52 | void *operator new(size_t, void *, void *); |
| 53 | void *testCustomNewMalloc() { |
| 54 | int *x = (int *)malloc(sizeof(int)); |
| 55 | |
| 56 | // Should be no-warning (the custom allocator could have freed x). |
| 57 | void *y = new (0, x) int; // no-warning |
| 58 | |
| 59 | return y; |
| 60 | } |
| 61 | |
Jordan Rose | 89e5aaf | 2012-07-16 23:38:09 +0000 | [diff] [blame] | 62 | void testScalarInitialization() { |
| 63 | int *n = new int(3); |
| 64 | clang_analyzer_eval(*n == 3); // expected-warning{{TRUE}} |
| 65 | |
| 66 | new (n) int(); |
| 67 | clang_analyzer_eval(*n == 0); // expected-warning{{TRUE}} |
| 68 | |
| 69 | new (n) int{3}; |
| 70 | clang_analyzer_eval(*n == 3); // expected-warning{{TRUE}} |
| 71 | |
| 72 | new (n) int{}; |
| 73 | clang_analyzer_eval(*n == 0); // expected-warning{{TRUE}} |
| 74 | } |
| 75 | |
Jordan Rose | 3c4e76d | 2012-06-20 05:34:32 +0000 | [diff] [blame] | 76 | |
Jordan Rose | c210cb7 | 2012-08-27 17:50:07 +0000 | [diff] [blame] | 77 | struct PtrWrapper { |
| 78 | int *x; |
| 79 | |
| 80 | PtrWrapper(int *input) : x(input) {} |
| 81 | }; |
| 82 | |
| 83 | PtrWrapper *testNewInvalidation() { |
| 84 | // Ensure that we don't consider this a leak. |
| 85 | return new PtrWrapper(static_cast<int *>(malloc(4))); |
| 86 | } |
| 87 | |
| 88 | |
Jordan Rose | 3c4e76d | 2012-06-20 05:34:32 +0000 | [diff] [blame] | 89 | //-------------------------------- |
| 90 | // Incorrectly-modelled behavior |
| 91 | //-------------------------------- |
| 92 | |
Jordan Rose | 89e5aaf | 2012-07-16 23:38:09 +0000 | [diff] [blame] | 93 | int testNoInitialization() { |
Jordan Rose | 3c4e76d | 2012-06-20 05:34:32 +0000 | [diff] [blame] | 94 | int *n = new int; |
| 95 | |
| 96 | // Should warn that *n is uninitialized. |
| 97 | if (*n) { // no-warning |
Jordan Rose | 89e5aaf | 2012-07-16 23:38:09 +0000 | [diff] [blame] | 98 | return 0; |
Jordan Rose | 3c4e76d | 2012-06-20 05:34:32 +0000 | [diff] [blame] | 99 | } |
Jordan Rose | 89e5aaf | 2012-07-16 23:38:09 +0000 | [diff] [blame] | 100 | return 1; |
Jordan Rose | 3c4e76d | 2012-06-20 05:34:32 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Jordan Rose | 89e5aaf | 2012-07-16 23:38:09 +0000 | [diff] [blame] | 103 | int testNoInitializationPlacement() { |
| 104 | int n; |
| 105 | new (&n) int; |
Jordan Rose | 3c4e76d | 2012-06-20 05:34:32 +0000 | [diff] [blame] | 106 | |
Jordan Rose | 89e5aaf | 2012-07-16 23:38:09 +0000 | [diff] [blame] | 107 | // Should warn that n is uninitialized. |
| 108 | if (n) { // no-warning |
| 109 | return 0; |
| 110 | } |
| 111 | return 1; |
Jordan Rose | 3c4e76d | 2012-06-20 05:34:32 +0000 | [diff] [blame] | 112 | } |