new.cpp revision 3c4e76d712eac172b100bb10b96637ffca105433
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-store region -verify %s 2 3void clang_analyzer_eval(bool); 4 5typedef typeof(sizeof(int)) size_t; 6extern "C" void *malloc(size_t); 7 8// This is the standard placement new. 9inline void* operator new(size_t, void* __p) throw() 10{ 11 return __p; 12} 13 14void *testPlacementNew() { 15 int *x = (int *)malloc(sizeof(int)); 16 *x = 1; 17 clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}}; 18 19 void *y = new (x) int; 20 clang_analyzer_eval(x == y); // expected-warning{{TRUE}}; 21 clang_analyzer_eval(*x == 1); // expected-warning{{UNKNOWN}}; 22 23 return y; 24} 25 26void *operator new(size_t, size_t, int *); 27void *testCustomNew() { 28 int x[1] = {1}; 29 clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}}; 30 31 void *y = new (0, x) int; 32 clang_analyzer_eval(*x == 1); // expected-warning{{UNKNOWN}}; 33 34 return y; // no-warning 35} 36 37 38//-------------------------------- 39// Incorrectly-modelled behavior 40//-------------------------------- 41 42void testZeroInitialization() { 43 int *n = new int; 44 45 // Should warn that *n is uninitialized. 46 if (*n) { // no-warning 47 } 48} 49 50void testValueInitialization() { 51 int *n = new int(3); 52 53 // Should be TRUE (and have no uninitialized variable warning) 54 clang_analyzer_eval(*n == 3); // expected-warning{{UNKNOWN}} 55} 56 57 58void *operator new(size_t, void *, void *); 59void *testCustomNewMalloc() { 60 int *x = (int *)malloc(sizeof(int)); 61 62 // Should be no-warning (the custom allocator could have freed x). 63 void *y = new (0, x) int; // expected-warning{{leak of memory pointed to by 'x'}} 64 65 return y; 66} 67 68