null-deref-path-notes.m
2.31 KB
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
// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-store=region -analyzer-output=text -fblocks -verify -Wno-objc-root-class %s
// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-store=region -analyzer-output=plist-multi-file -fblocks -Wno-objc-root-class %s -o %t
// RUN: %normalize_plist <%t | diff -ub %S/Inputs/expected-plists/null-deref-path-notes.m.plist -
@interface Root {
@public
int uniqueID;
}
- (id)initWithID:(int)newID;
- (void)refreshID;
@end
int testNull(Root *obj) {
if (obj) return 0;
// expected-note@-1 {{Assuming 'obj' is nil}}
// expected-note@-2 {{Taking false branch}}
int *x = &obj->uniqueID; // expected-note{{'x' initialized to a null pointer value}}
return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} expected-note{{Dereference of null pointer (loaded from variable 'x')}}
}
@interface Subclass : Root
@end
@implementation Subclass
- (id)initWithID:(int)newID {
self = [super initWithID:newID]; // expected-note{{Value assigned to 'self'}}
if (self) return self;
// expected-note@-1 {{Assuming 'self' is nil}}
// expected-note@-2 {{Taking false branch}}
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')}}
return self;
}
@end
void repeatedStores(int coin) {
int *p = 0;
if (coin) {
// expected-note@-1 {{Assuming 'coin' is 0}}
// expected-note@-2 {{Taking false branch}}
extern int *getPointer();
p = getPointer();
} else {
p = 0; // expected-note {{Null pointer value stored to 'p'}}
}
*p = 1; // expected-warning{{Dereference of null pointer}} expected-note{{Dereference of null pointer}}
}
@interface WithArrayPtr
- (void) useArray;
@end
@implementation WithArrayPtr {
@public int *p;
}
- (void)useArray {
p[1] = 2; // expected-warning{{Array access (via ivar 'p') results in a null pointer dereference}}
// expected-note@-1{{Array access (via ivar 'p') results in a null pointer dereference}}
}
@end
void testWithArrayPtr(WithArrayPtr *w) {
w->p = 0; // expected-note{{Null pointer value stored to 'p'}}
[w useArray]; // expected-note{{Calling 'useArray'}}
}