57 lines · cpp
1// RUN: %clang_analyze_cc1 -std=c++14 -analyzer-checker=core \2// RUN: -analyzer-config suppress-null-return-paths=false \3// RUN: -verify %s4// RUN: %clang_analyze_cc1 -std=c++14 -analyzer-checker=core \5// RUN: -DSUPPRESSED \6// RUN: -verify %s7 8void clang_analyzer_eval(bool);9 10typedef __typeof__(sizeof(int)) size_t;11 12 13// These are ill-formed. One cannot return nullptr from a throwing version of an14// operator new.15void *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}20void *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 26struct S {27 int x;28 S() : x(1) {}29 ~S() {}30 int getX() const { return x; }31};32 33void testArrays() {34 S *s = new S[10]; // no-crash35 s[0].x = 2;36#ifndef SUPPRESSED37 // expected-warning@-2 {{Dereference of null pointer}}38#endif39}40 41void testCtor() {42 S *s = new S();43 s->x = 13;44#ifndef SUPPRESSED45 // expected-warning@-2 {{Access to field 'x' results in a dereference of a null pointer (loaded from variable 's')}}46#endif47}48 49void testMethod() {50 S *s = new S();51 const int X = s->getX();52#ifndef SUPPRESSED53 // expected-warning@-2 {{Called C++ object pointer is null}}54#endif55}56 57