reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-output=text -analyzer-config c++-inlining=destructors -std=c++11 -verify -Wno-tautological-undefined-compare %s
// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-output=plist-multi-file -analyzer-config c++-inlining=destructors -std=c++11 %s -o %t.plist -Wno-tautological-undefined-compare
// RUN: %normalize_plist <%t.plist | diff -ub %S/Inputs/expected-plists/path-notes.cpp.plist -

class Foo {
public:
  static void use(int *p) {
    *p = 1; // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
    // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
  }

  Foo(int *p) {
    use(p);
    // expected-note@-1 {{Passing null pointer value via 1st parameter 'p'}}
    // expected-note@-2 {{Calling 'Foo::use'}}
  }
};

static int *globalPtr;

class Bar {
public:
  ~Bar() {
    Foo f(globalPtr);
    // expected-note@-1 {{Passing null pointer value via 1st parameter 'p'}}
    // expected-note@-2 {{Calling constructor for 'Foo'}}
  }
};

void test() {
  Bar b;
  globalPtr = 0;
  // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
} // expected-note {{Calling '~Bar'}}


void testAnonymous() {
  class {
  public:
    void method(int *p) {
      *p = 1; // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
      // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
    }
  } anonymous;

  anonymous.method(0);
  // expected-note@-1 {{Passing null pointer value via 1st parameter 'p'}}
  // expected-note@-2 {{Calling 'method'}}
}


// A simplified version of std::move.
template <typename T>
T &&move(T &obj) {
  return static_cast<T &&>(obj);
}


namespace defaulted {
  class Dereferencer {
  public:
    Dereferencer() {
      *globalPtr = 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
      // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
    }

    Dereferencer(const Dereferencer &Other) {
      *globalPtr = 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
      // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
    }

    Dereferencer(Dereferencer &&Other) {
      *globalPtr = 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
      // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
    }

    void operator=(const Dereferencer &Other) {
      *globalPtr = 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
      // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
    }

    void operator=(Dereferencer &&Other) {
      *globalPtr = 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
      // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
    }

    ~Dereferencer() {
      *globalPtr = 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
      // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
    }
  };

  class Wrapper {
    Dereferencer d;
  };

  class MovableWrapper {
    Dereferencer d;
  public:
    MovableWrapper() = default;

    MovableWrapper(MovableWrapper &&Other) = default;
    // expected-note@-1 {{Calling move constructor for 'Dereferencer'}}

    MovableWrapper &operator=(MovableWrapper &&Other) = default;
    // expected-note@-1 {{Calling move assignment operator for 'Dereferencer'}}
  };

  void testDefaultConstruction() {
    globalPtr = 0;
    // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
    Wrapper w;
    // expected-note@-1 {{Calling implicit default constructor for 'Wrapper'}}
    // expected-note@-2 {{Calling default constructor for 'Dereferencer'}}
  }

  void testCopyConstruction(const Wrapper &input) {
    globalPtr = 0;
    // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
    Wrapper w{input};
    // expected-note@-1 {{Calling implicit copy constructor for 'Wrapper'}}
    // expected-note@-2 {{Calling copy constructor for 'Dereferencer'}}
  }

  void testMoveConstruction(MovableWrapper &&input) {
    globalPtr = 0;
    // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
    MovableWrapper w{move(input)};
    // expected-note@-1 {{Calling defaulted move constructor for 'MovableWrapper'}}
  }

  void testCopyAssignment(const Wrapper &input) {
    Wrapper w;
    globalPtr = 0;
    // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
    w = input;
    // expected-note@-1 {{Calling implicit copy assignment operator for 'Wrapper'}}
    // expected-note@-2 {{Calling copy assignment operator for 'Dereferencer'}}
  }

  void testMoveAssignment(MovableWrapper &&input) {
    MovableWrapper w;
    globalPtr = 0;
    // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
    w = move(input);
    // expected-note@-1 {{Calling defaulted move assignment operator for 'MovableWrapper'}}
  }

  void testDestruction() {
    Wrapper w;
    globalPtr = 0;
    // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
  }
  // expected-note@-1 {{Calling implicit destructor for 'Wrapper'}}
  // expected-note@-2 {{Calling '~Dereferencer'}}
}

namespace ReturnZeroNote {
  int getZero() {
    return 0;
    // expected-note@-1 {{Returning zero}}
  }

  const int &getZeroByRef() {
    static int zeroVar;
    zeroVar = 0;
    // expected-note@-1 {{The value 0 is assigned to 'zeroVar'}}
    return zeroVar;
    // expected-note@-1 {{Returning zero (reference to 'zeroVar')}}
  }

  void test() {
    int problem = 1 / getZero(); // expected-warning {{Division by zero}}
    // expected-note@-1 {{Calling 'getZero'}}
    // expected-note@-2 {{Returning from 'getZero'}}
    // expected-note@-3 {{Division by zero}}
  }

  void testRef() {
    int problem = 1 / getZeroByRef(); // expected-warning {{Division by zero}}
    // expected-note@-1 {{Calling 'getZeroByRef'}}
    // expected-note@-2 {{Returning from 'getZeroByRef'}}
    // expected-note@-3 {{Division by zero}}
  }
}

int &returnNullReference() {
  int *x = 0;
  // expected-note@-1 {{'x' initialized to a null pointer value}}
  return *x; // expected-warning{{Returning null reference}}
  // expected-note@-1 {{Returning null reference}}
}

struct FooWithInitializer {
	int *ptr;
	FooWithInitializer(int *p) : ptr(p) { // expected-note {{Null pointer value stored to 'f.ptr'}}
		*ptr = 1; // expected-note {{Dereference of null pointer (loaded from field 'ptr')}}
    // expected-warning@-1 {{Dereference of null pointer (loaded from field 'ptr')}}
	}
};

void testPathNoteOnInitializer() {
	int *p = 0; // expected-note {{'p' initialized to a null pointer value}}

	FooWithInitializer f(p); // expected-note {{Passing null pointer value via 1st parameter 'p'}}
  // expected-note@-1 {{Calling constructor for 'FooWithInitializer'}}
}

int testNonPrintableAssignment(int **p) {
  int *&y = *p; // expected-note {{'y' initialized here}}
  y = 0;        // expected-note {{Storing null pointer value}}
  return *y; // expected-warning {{Dereference of null pointer (loaded from variable 'y')}}
             // expected-note@-1 {{Dereference of null pointer (loaded from variable 'y')}}
}

struct Base { int *x; };
struct Derived : public Base {};

void test(Derived d) {
  d.x = 0; //expected-note {{Null pointer value stored to 'd.x'}}
  *d.x = 1; // expected-warning {{Dereference of null pointer (loaded from field 'x')}}
            // expected-note@-1 {{Dereference of null pointer (loaded from field 'x')}}
}

struct Owner {
	struct Wrapper {
		int x;
	};
	Wrapper *arr;
	void testGetDerefExprOnMemberExprWithADot();
};

void Owner::testGetDerefExprOnMemberExprWithADot() {
	if (arr)  // expected-note {{Assuming field 'arr' is null}}
            // expected-note@-1 {{Taking false branch}}
	  ;
	arr[1].x = 1; //expected-warning {{Dereference of null pointer}}
                //expected-note@-1 {{Dereference of null pointer}}
}

void testGetDerefExprOnMemberExprWithADot() {
  Owner::Wrapper *arr; // expected-note {{'arr' declared without an initial value}}
	arr[2].x = 1; // expected-warning {{Dereference of undefined pointer value}}
                // expected-note@-1 {{Dereference of undefined pointer value}}
}



class A {
public:
  void bar() const {}
};
const A& testDeclRefExprToReferenceInGetDerefExpr(const A *ptr) {
  const A& val = *ptr; //expected-note {{'val' initialized here}}

  // This is not valid C++; if 'ptr' were null, creating 'ref' would be illegal.
  // However, this is not checked at runtime, so this branch is actually
  // possible.
  if (&val == 0) { //expected-note {{Assuming pointer value is null}}
                   // expected-note@-1 {{Taking true branch}}
    val.bar(); // expected-warning {{Called C++ object pointer is null}}
               // expected-note@-1 {{Called C++ object pointer is null}}
  }

  return val;
}

int generateNoteOnDefaultArgument(int one, int two = 0) {
  return one/two; // expected-warning {{Division by zero}}
                  // expected-note@-1 {{Division by zero}}
}
int callGenerateNoteOnDefaultArgument(int o) {
  return generateNoteOnDefaultArgument(o); //expected-note{{Calling 'generateNoteOnDefaultArgument'}}
                                           //expected-note@-1 {{Passing the value 0 via 2nd parameter 'two'}}
}

namespace PR17746 {
  class Inner {
  public:
    ~Inner() {
      *(volatile int *)0 = 1; // expected-warning {{Dereference of null pointer}}
      // expected-note@-1 {{Dereference of null pointer}}
    }
  };

  class Outer {
  public:
    Inner *inner;
    ~Outer() {
      delete inner;
      // expected-note@-1 {{Calling '~Inner'}}
    }
  };

  void test(Outer *outer) {
    delete outer;
    // expected-note@-1 {{Calling '~Outer'}}
  }
}