1 // RUN: %clang_analyze_cc1 -analyzer-checker=core \ 2 // RUN: -analyzer-config suppress-null-return-paths=false \ 3 // RUN: -verify %s 4 // RUN: %clang_analyze_cc1 -analyzer-checker=core \ 5 // RUN: -DSUPPRESSED \ 6 // RUN: -verify %s 7 8 void clang_analyzer_eval(bool); 9 10 typedef __typeof__(sizeof(int)) size_t; 11 12 13 // These are ill-formed. One cannot return nullptr from a throwing version of an 14 // operator new. 15 void *operator new(size_t size) { 16 return nullptr; 17 // expected-warning@-1 {{'operator new' should not return a null pointer unless it is declared 'throw()' or 'noexcept'}} 18 } 19 void *operator new[](size_t size) { 20 return nullptr; 21 // expected-warning@-1 {{'operator new[]' should not return a null pointer unless it is declared 'throw()' or 'noexcept'}} 22 } 23 24 struct S { 25 int x; 26 S() : x(1) {} 27 ~S() {} 28 int getX() const { return x; } 29 }; 30 31 void testArrays() { 32 S *s = new S[10]; // no-crash 33 s[0].x = 2; 34 #ifndef SUPPRESSED 35 // expected-warning@-2 {{Dereference of null pointer}} 36 #endif 37 } 38 39 void testCtor() { 40 S *s = new S(); 41 s->x = 13; 42 #ifndef SUPPRESSED 43 // expected-warning@-2 {{Access to field 'x' results in a dereference of a null pointer (loaded from variable 's')}} 44 #endif 45 } 46 47 void testMethod() { 48 S *s = new S(); 49 const int X = s->getX(); 50 #ifndef SUPPRESSED 51 // expected-warning@-2 {{Called C++ object pointer is null}} 52 #endif 53 } 54 55