Jordan Rose | e38c1c2 | 2012-06-20 01:32:01 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-store region -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 | |
| 5 | typedef typeof(sizeof(int)) size_t; |
| 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 | 3c4e76d | 2012-06-20 05:34:32 +0000 | [diff] [blame] | 62 | |
| 63 | //-------------------------------- |
| 64 | // Incorrectly-modelled behavior |
| 65 | //-------------------------------- |
| 66 | |
| 67 | void testZeroInitialization() { |
| 68 | int *n = new int; |
| 69 | |
| 70 | // Should warn that *n is uninitialized. |
| 71 | if (*n) { // no-warning |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void testValueInitialization() { |
| 76 | int *n = new int(3); |
| 77 | |
| 78 | // Should be TRUE (and have no uninitialized variable warning) |
| 79 | clang_analyzer_eval(*n == 3); // expected-warning{{UNKNOWN}} |
| 80 | } |
| 81 | |