Clang Project

clang_source_code/test/Analysis/new-ctor-null.cpp
1// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -analyzer-config c++-allocator-inlining=true -std=c++11 -verify %s
2
3void clang_analyzer_eval(bool);
4void clang_analyzer_warnIfReached();
5
6typedef __typeof__(sizeof(int)) size_t;
7
8void *operator new(size_t size) throw() {
9  return nullptr;
10}
11void *operator new[](size_t size) throw() {
12  return nullptr;
13}
14
15struct S {
16  int x;
17  S() : x(1) {
18    // FIXME: Constructor should not be called with null this, even if it was
19    // returned by operator new().
20    clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
21  }
22  ~S() {}
23};
24
25void testArrays() {
26  S *s = new S[10]; // no-crash
27  s[0].x = 2; // expected-warning{{Dereference of null pointer}}
28}
29
30int global;
31void testInvalidationOnConstructionIntoNull() {
32  global = 0;
33  S *s = new S();
34  // FIXME: Should be FALSE - we should not invalidate globals.
35  clang_analyzer_eval(global); // expected-warning{{UNKNOWN}}
36}
37