xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/inlining/inline-defensive-checks.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-config suppress-inlined-defensive-checks=true -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // Perform inline defensive checks.
idc(int * p)4*f4a2713aSLionel Sambuc void idc(int *p) {
5*f4a2713aSLionel Sambuc 	if (p)
6*f4a2713aSLionel Sambuc 		;
7*f4a2713aSLionel Sambuc }
8*f4a2713aSLionel Sambuc 
test01(int * p)9*f4a2713aSLionel Sambuc int test01(int *p) {
10*f4a2713aSLionel Sambuc   if (p)
11*f4a2713aSLionel Sambuc     ;
12*f4a2713aSLionel Sambuc   return *p; // expected-warning {{Dereference of null pointer}}
13*f4a2713aSLionel Sambuc }
14*f4a2713aSLionel Sambuc 
test02(int * p,int * x)15*f4a2713aSLionel Sambuc int test02(int *p, int *x) {
16*f4a2713aSLionel Sambuc   if (p)
17*f4a2713aSLionel Sambuc     ;
18*f4a2713aSLionel Sambuc   idc(p);
19*f4a2713aSLionel Sambuc 	if (x)
20*f4a2713aSLionel Sambuc 		;
21*f4a2713aSLionel Sambuc   return *p; // expected-warning {{Dereference of null pointer}}
22*f4a2713aSLionel Sambuc }
23*f4a2713aSLionel Sambuc 
test03(int * p,int * x)24*f4a2713aSLionel Sambuc int test03(int *p, int *x) {
25*f4a2713aSLionel Sambuc 	idc(p);
26*f4a2713aSLionel Sambuc 	if (p)
27*f4a2713aSLionel Sambuc 		;
28*f4a2713aSLionel Sambuc 	return *p; // False negative
29*f4a2713aSLionel Sambuc }
30*f4a2713aSLionel Sambuc 
deref04(int * p)31*f4a2713aSLionel Sambuc int deref04(int *p) {
32*f4a2713aSLionel Sambuc   return *p; // expected-warning {{Dereference of null pointer}}
33*f4a2713aSLionel Sambuc }
34*f4a2713aSLionel Sambuc 
test04(int * p)35*f4a2713aSLionel Sambuc int test04(int *p) {
36*f4a2713aSLionel Sambuc   if (p)
37*f4a2713aSLionel Sambuc     ;
38*f4a2713aSLionel Sambuc   idc(p);
39*f4a2713aSLionel Sambuc   return deref04(p);
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc 
test11(int * q,int * x)42*f4a2713aSLionel Sambuc int test11(int *q, int *x) {
43*f4a2713aSLionel Sambuc 	int *p = q;
44*f4a2713aSLionel Sambuc 	if (q)
45*f4a2713aSLionel Sambuc 		;
46*f4a2713aSLionel Sambuc 	if (x)
47*f4a2713aSLionel Sambuc 		;
48*f4a2713aSLionel Sambuc 	return *p; // expected-warning{{Dereference of null pointer}}
49*f4a2713aSLionel Sambuc }
50*f4a2713aSLionel Sambuc 
test12(int * q)51*f4a2713aSLionel Sambuc int test12(int *q) {
52*f4a2713aSLionel Sambuc 	int *p = q;
53*f4a2713aSLionel Sambuc 	idc(q);
54*f4a2713aSLionel Sambuc 	return *p;
55*f4a2713aSLionel Sambuc }
56*f4a2713aSLionel Sambuc 
test13(int * q)57*f4a2713aSLionel Sambuc int test13(int *q) {
58*f4a2713aSLionel Sambuc 	int *p = q;
59*f4a2713aSLionel Sambuc 	idc(p);
60*f4a2713aSLionel Sambuc 	return *p;
61*f4a2713aSLionel Sambuc }
62*f4a2713aSLionel Sambuc 
test21(int * q,int * x)63*f4a2713aSLionel Sambuc int test21(int *q, int *x) {
64*f4a2713aSLionel Sambuc 	if (q)
65*f4a2713aSLionel Sambuc 		;
66*f4a2713aSLionel Sambuc 	if (x)
67*f4a2713aSLionel Sambuc 		;
68*f4a2713aSLionel Sambuc 	int *p = q;
69*f4a2713aSLionel Sambuc 	return *p; // expected-warning{{Dereference of null pointer}}
70*f4a2713aSLionel Sambuc }
71*f4a2713aSLionel Sambuc 
test22(int * q,int * x)72*f4a2713aSLionel Sambuc int test22(int *q, int *x) {
73*f4a2713aSLionel Sambuc   idc(q);
74*f4a2713aSLionel Sambuc 	if (x)
75*f4a2713aSLionel Sambuc 		;
76*f4a2713aSLionel Sambuc 	int *p = q;
77*f4a2713aSLionel Sambuc 	return *p;
78*f4a2713aSLionel Sambuc }
79*f4a2713aSLionel Sambuc 
test23(int * q,int * x)80*f4a2713aSLionel Sambuc int test23(int *q, int *x) {
81*f4a2713aSLionel Sambuc   idc(q);
82*f4a2713aSLionel Sambuc 	if (x)
83*f4a2713aSLionel Sambuc 		;
84*f4a2713aSLionel Sambuc 	int *p = q;
85*f4a2713aSLionel Sambuc   if (!p)
86*f4a2713aSLionel Sambuc     ;
87*f4a2713aSLionel Sambuc 	return *p; // False negative
88*f4a2713aSLionel Sambuc }
89*f4a2713aSLionel Sambuc 
use(char * p)90*f4a2713aSLionel Sambuc void use(char *p) {
91*f4a2713aSLionel Sambuc   if (!p)
92*f4a2713aSLionel Sambuc     return;
93*f4a2713aSLionel Sambuc   p[0] = 'a';
94*f4a2713aSLionel Sambuc }
95*f4a2713aSLionel Sambuc 
test24(char * buffer)96*f4a2713aSLionel Sambuc void test24(char *buffer) {
97*f4a2713aSLionel Sambuc   use(buffer);
98*f4a2713aSLionel Sambuc   buffer[1] = 'b';
99*f4a2713aSLionel Sambuc }
100*f4a2713aSLionel Sambuc 
101*f4a2713aSLionel Sambuc // Ensure idc works on pointers with constant offset.
idcchar(const char * s2)102*f4a2713aSLionel Sambuc void idcchar(const char *s2) {
103*f4a2713aSLionel Sambuc   if(s2)
104*f4a2713aSLionel Sambuc     ;
105*f4a2713aSLionel Sambuc }
testConstantOffset(char * value)106*f4a2713aSLionel Sambuc void testConstantOffset(char *value) {
107*f4a2713aSLionel Sambuc   char *cursor = value + 5;
108*f4a2713aSLionel Sambuc   idcchar(cursor);
109*f4a2713aSLionel Sambuc   if (*cursor) {
110*f4a2713aSLionel Sambuc     cursor++;
111*f4a2713aSLionel Sambuc   }
112*f4a2713aSLionel Sambuc }
113*f4a2713aSLionel Sambuc 
114*f4a2713aSLionel Sambuc // Ensure idc works for integer zero values (ex: suppressed div by zero).
idcZero(int assume)115*f4a2713aSLionel Sambuc void idcZero(int assume) {
116*f4a2713aSLionel Sambuc   if (assume)
117*f4a2713aSLionel Sambuc     ;
118*f4a2713aSLionel Sambuc }
119*f4a2713aSLionel Sambuc 
idcTriggerZeroValue(int m)120*f4a2713aSLionel Sambuc int idcTriggerZeroValue(int m) {
121*f4a2713aSLionel Sambuc   idcZero(m);
122*f4a2713aSLionel Sambuc   return 5/m; // no-warning
123*f4a2713aSLionel Sambuc }
124*f4a2713aSLionel Sambuc 
idcTriggerZeroValueThroughCall(int i)125*f4a2713aSLionel Sambuc int idcTriggerZeroValueThroughCall(int i) {
126*f4a2713aSLionel Sambuc   return 5/i; // no-warning
127*f4a2713aSLionel Sambuc }
idcTrackZeroValueThroughCall(int x)128*f4a2713aSLionel Sambuc void idcTrackZeroValueThroughCall(int x) {
129*f4a2713aSLionel Sambuc   idcZero(x);
130*f4a2713aSLionel Sambuc   idcTriggerZeroValueThroughCall(x);
131*f4a2713aSLionel Sambuc }
132*f4a2713aSLionel Sambuc 
idcTriggerZeroThroughDoubleAssignemnt(int i)133*f4a2713aSLionel Sambuc int idcTriggerZeroThroughDoubleAssignemnt(int i) {
134*f4a2713aSLionel Sambuc   return 5/i; // no-warning
135*f4a2713aSLionel Sambuc }
idcTrackZeroThroughDoubleAssignemnt(int x)136*f4a2713aSLionel Sambuc void idcTrackZeroThroughDoubleAssignemnt(int x) {
137*f4a2713aSLionel Sambuc   idcZero(x);
138*f4a2713aSLionel Sambuc   int y = x;
139*f4a2713aSLionel Sambuc   int z = y;
140*f4a2713aSLionel Sambuc   idcTriggerZeroValueThroughCall(z);
141*f4a2713aSLionel Sambuc }
142