1// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-store=region -analyzer-output=text -fblocks -verify -Wno-objc-root-class %s
2// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-store=region -analyzer-output=plist-multi-file -fblocks -Wno-objc-root-class %s -o %t
3// RUN: %normalize_plist <%t | diff -ub %S/Inputs/expected-plists/null-deref-path-notes.m.plist -
4
5@interface Root {
6@public
7  int uniqueID;
8}
9- (id)initWithID:(int)newID;
10- (void)refreshID;
11@end
12
13int testNull(Root *obj) {
14  if (obj) return 0;
15  // expected-note@-1 {{Assuming 'obj' is nil}}
16  // expected-note@-2 {{Taking false branch}}
17
18  int *x = &obj->uniqueID; // expected-note{{'x' initialized to a null pointer value}}
19  return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} expected-note{{Dereference of null pointer (loaded from variable 'x')}}
20}
21
22
23@interface Subclass : Root
24@end
25
26@implementation Subclass
27- (id)initWithID:(int)newID {
28  self = [super initWithID:newID]; // expected-note{{Value assigned to 'self'}}
29  if (self) return self;
30  // expected-note@-1 {{Assuming 'self' is nil}}
31  // expected-note@-2 {{Taking false branch}}
32
33  uniqueID = newID; // expected-warning{{Access to instance variable 'uniqueID' results in a dereference of a null pointer (loaded from variable 'self')}} expected-note{{Access to instance variable 'uniqueID' results in a dereference of a null pointer (loaded from variable 'self')}}
34  return self;
35}
36
37@end
38
39void repeatedStores(int coin) {
40  int *p = 0;
41  if (coin) {
42    // expected-note@-1 {{Assuming 'coin' is 0}}
43    // expected-note@-2 {{Taking false branch}}
44    extern int *getPointer();
45    p = getPointer();
46  } else {
47    p = 0; // expected-note {{Null pointer value stored to 'p'}}
48  }
49
50  *p = 1; // expected-warning{{Dereference of null pointer}} expected-note{{Dereference of null pointer}}
51}
52
53@interface WithArrayPtr
54- (void) useArray;
55@end
56
57@implementation WithArrayPtr {
58@public int *p;
59}
60- (void)useArray {
61  p[1] = 2; // expected-warning{{Array access (via ivar 'p') results in a null pointer dereference}}
62            // expected-note@-1{{Array access (via ivar 'p') results in a null pointer dereference}}
63}
64@end
65
66void testWithArrayPtr(WithArrayPtr *w) {
67  w->p = 0; // expected-note{{Null pointer value stored to 'p'}}
68  [w useArray]; // expected-note{{Calling 'useArray'}}
69}
70
71