1 // RUN: %clang_analyze_cc1 %s -analyzer-checker=core.NullDereference -analyzer-output=text -verify
2 // RUN: %clang_analyze_cc1 %s -analyzer-checker=core.NullDereference -analyzer-output=plist -o %t
3 // RUN: %normalize_plist <%t | diff -ub %S/Inputs/expected-plists/conditional-path-notes.c.plist -
4 
testCondOp(int * p)5 void testCondOp(int *p) {
6   int *x = p ? p : p;
7   // expected-note@-1 {{Assuming 'p' is null}}
8   // expected-note@-2 {{'?' condition is false}}
9   // expected-note@-3 {{'x' initialized to a null pointer value}}
10   *x = 1; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
11   // expected-note@-1 {{Dereference of null pointer (loaded from variable 'x')}}
12 }
13 
testCondProblem(int * p)14 void testCondProblem(int *p) {
15   if (p) return;
16   // expected-note@-1 {{Assuming 'p' is null}}
17   // expected-note@-2 {{Taking false branch}}
18 
19   int x = *p ? 0 : 1; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}}
20   // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
21   (void)x;
22 }
23 
testLHSProblem(int * p)24 void testLHSProblem(int *p) {
25   int x = !p ? *p : 1; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}}
26   // expected-note@-1 {{Assuming 'p' is null}}
27   // expected-note@-2 {{'?' condition is true}}
28   // expected-note@-3 {{Dereference of null pointer (loaded from variable 'p')}}
29   (void)x;
30 }
31 
testRHSProblem(int * p)32 void testRHSProblem(int *p) {
33   int x = p ? 1 : *p; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}}
34   // expected-note@-1 {{Assuming 'p' is null}}
35   // expected-note@-2 {{'?' condition is false}}
36   // expected-note@-3 {{Dereference of null pointer (loaded from variable 'p')}}
37   (void)x;
38 }
39 
testBinaryCondOp(int * p)40 void testBinaryCondOp(int *p) {
41   int *x = p ?: p;
42   // expected-note@-1 {{'?' condition is false}}
43   // expected-note@-2 {{'x' initialized to a null pointer value}}
44   *x = 1; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
45   // expected-note@-1 {{Dereference of null pointer (loaded from variable 'x')}}
46 }
47 
testBinaryLHSProblem(int * p)48 void testBinaryLHSProblem(int *p) {
49   if (p) return;
50   // expected-note@-1 {{Assuming 'p' is null}}
51   // expected-note@-2 {{Taking false branch}}
52 
53   int x = *p ?: 1; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}}
54   // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
55   (void)x;
56 }
57 
testDiagnosableBranch(int a)58 void testDiagnosableBranch(int a) {
59   if (a) {
60     // expected-note@-1 {{Assuming 'a' is not equal to 0}}
61     // expected-note@-2 {{Taking true branch}}
62     *(volatile int *)0 = 1; // expected-warning{{Dereference of null pointer}}
63     // expected-note@-1 {{Dereference of null pointer}}
64   }
65 }
66 
testDiagnosableBranchLogical(int a,int b)67 void testDiagnosableBranchLogical(int a, int b) {
68   if (a && b) {
69     // expected-note@-1 {{Assuming 'a' is not equal to 0}}
70     // expected-note@-2 {{Left side of '&&' is true}}
71     // expected-note@-3 {{Assuming 'b' is not equal to 0}}
72     // expected-note@-4 {{Taking true branch}}
73     *(volatile int *)0 = 1; // expected-warning{{Dereference of null pointer}}
74     // expected-note@-1 {{Dereference of null pointer}}
75   }
76 }
77 
testNonDiagnosableBranchArithmetic(int a,int b)78 void testNonDiagnosableBranchArithmetic(int a, int b) {
79   if (a - b) {
80     // expected-note@-1 {{Taking true branch}}
81     // expected-note@-2 {{Assuming the condition is true}}
82     *(volatile int *)0 = 1; // expected-warning{{Dereference of null pointer}}
83     // expected-note@-1 {{Dereference of null pointer}}
84   }
85 }
86 
87