1 // RUN: %clang_analyze_cc1 -std=c++14 -analyzer-checker=core \
2 // RUN:  -analyzer-config suppress-null-return-paths=false \
3 // RUN:  -verify %s
4 // RUN: %clang_analyze_cc1 -std=c++14 -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.
operator new(size_t size)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   // expected-warning@-2 {{null returned from function that requires a non-null return value}}
19 }
operator new[](size_t size)20 void *operator new[](size_t size) {
21   return nullptr;
22   // expected-warning@-1 {{'operator new[]' should not return a null pointer unless it is declared 'throw()' or 'noexcept'}}
23   // expected-warning@-2 {{null returned from function that requires a non-null return value}}
24 }
25 
26 struct S {
27   int x;
SS28   S() : x(1) {}
~SS29   ~S() {}
getXS30   int getX() const { return x; }
31 };
32 
testArrays()33 void testArrays() {
34   S *s = new S[10]; // no-crash
35   s[0].x = 2;
36 #ifndef SUPPRESSED
37   // expected-warning@-2 {{Dereference of null pointer}}
38 #endif
39 }
40 
testCtor()41 void testCtor() {
42   S *s = new S();
43   s->x = 13;
44 #ifndef SUPPRESSED
45   // expected-warning@-2 {{Access to field 'x' results in a dereference of a null pointer (loaded from variable 's')}}
46 #endif
47 }
48 
testMethod()49 void testMethod() {
50   S *s = new S();
51   const int X = s->getX();
52 #ifndef SUPPRESSED
53   // expected-warning@-2 {{Called C++ object pointer is null}}
54 #endif
55 }
56 
57