]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/Analysis/uninit-vals-ps.c
Vendor import of clang trunk r338536:
[FreeBSD/FreeBSD.git] / test / Analysis / uninit-vals-ps.c
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-store=region -fblocks -verify %s
2
3 struct FPRec {
4   void (*my_func)(int * x);  
5 };
6
7 int bar(int x);
8
9 int f1_a(struct FPRec* foo) {
10   int x;
11   (*foo->my_func)(&x);
12   return bar(x)+1; // no-warning
13 }
14
15 int f1_b() {
16   int x;
17   return bar(x)+1;  // expected-warning{{1st function call argument is an uninitialized value}}
18 }
19
20 int f2() {
21   
22   int x;
23   
24   if (x+1)  // expected-warning{{The left operand of '+' is a garbage value}}
25     return 1;
26     
27   return 2;  
28 }
29
30 int f2_b() {
31   int x;
32   
33   return ((1+x)+2+((x))) + 1 ? 1 : 2; // expected-warning{{The right operand of '+' is a garbage value}}
34 }
35
36 int f3(void) {
37   int i;
38   int *p = &i;
39   if (*p > 0) // expected-warning{{The left operand of '>' is a garbage value}}
40     return 0;
41   else
42     return 1;
43 }
44
45 void f4_aux(float* x);
46 float f4(void) {
47   float x;
48   f4_aux(&x);
49   return x;  // no-warning
50 }
51
52 struct f5_struct { int x; };
53 void f5_aux(struct f5_struct* s);
54 int f5(void) {
55   struct f5_struct s;
56   f5_aux(&s);
57   return s.x; // no-warning
58 }
59
60 void f6(int x) {
61   int a[20];
62   if (x == 25) {}
63   if (a[x] == 123) {} // expected-warning{{The left operand of '==' is a garbage value due to array index out of bounds}}
64 }
65
66 int ret_uninit() {
67   int i;
68   int *p = &i;
69   return *p;  // expected-warning{{Undefined or garbage value returned to caller}}
70 }
71
72 // <rdar://problem/6451816>
73 typedef unsigned char Boolean;
74 typedef const struct __CFNumber * CFNumberRef;
75 typedef signed long CFIndex;
76 typedef CFIndex CFNumberType;
77 typedef unsigned long UInt32;
78 typedef UInt32 CFStringEncoding;
79 typedef const struct __CFString * CFStringRef;
80 extern Boolean CFNumberGetValue(CFNumberRef number, CFNumberType theType, void *valuePtr);
81 extern CFStringRef CFStringConvertEncodingToIANACharSetName(CFStringEncoding encoding);
82
83 CFStringRef rdar_6451816(CFNumberRef nr) {
84   CFStringEncoding encoding;
85   // &encoding is casted to void*.  This test case tests whether or not
86   // we properly invalidate the value of 'encoding'.
87   CFNumberGetValue(nr, 9, &encoding);
88   return CFStringConvertEncodingToIANACharSetName(encoding); // no-warning
89 }
90
91 // PR 4630 - false warning with nonnull attribute
92 //  This false positive (due to a regression) caused the analyzer to falsely
93 //  flag a "return of uninitialized value" warning in the first branch due to
94 //  the nonnull attribute.
95 void pr_4630_aux(char *x, int *y) __attribute__ ((nonnull (1)));
96 void pr_4630_aux_2(char *x, int *y);
97 int pr_4630(char *a, int y) {
98   int x;
99   if (y) {
100     pr_4630_aux(a, &x);
101     return x;   // no-warning
102   }
103   else {
104     pr_4630_aux_2(a, &x);
105     return x;   // no-warning
106   }
107 }
108
109 // PR 4631 - False positive with union initializer
110 //  Previously the analyzer didn't examine the compound initializers of unions,
111 //  resulting in some false positives for initializers with side-effects.
112 union u_4631 { int a; };
113 struct s_4631 { int a; };
114 int pr4631_f2(int *p);
115 int pr4631_f3(void *q);
116 int pr4631_f1(void)
117 {
118   int x;
119   union u_4631 m = { pr4631_f2(&x) };
120   pr4631_f3(&m); // tell analyzer that we use m
121   return x;  // no-warning
122 }
123 int pr4631_f1_b(void)
124 {
125   int x;
126   struct s_4631 m = { pr4631_f2(&x) };
127   pr4631_f3(&m); // tell analyzer that we use m
128   return x;  // no-warning
129 }
130
131 // <rdar://problem/12278788> - FP when returning a void-valued expression from
132 // a void function...or block.
133 void foo_radar12278788() { return; }
134 void test_radar12278788() {
135   return foo_radar12278788(); // no-warning
136 }
137
138 void foo_radar12278788_fp() { return; }
139 typedef int (*RetIntFuncType)();
140 typedef void (*RetVoidFuncType)();
141 int test_radar12278788_FP() {
142   RetVoidFuncType f = foo_radar12278788_fp;
143   return ((RetIntFuncType)f)(); //expected-warning {{Undefined or garbage value returned to caller}}
144 }
145
146 void rdar13665798() {
147   ^() {
148     return foo_radar12278788(); // no-warning
149   }();
150   ^void() {
151     return foo_radar12278788(); // no-warning
152   }();
153   ^int() {
154     RetVoidFuncType f = foo_radar12278788_fp;
155     return ((RetIntFuncType)f)(); //expected-warning {{Undefined or garbage value returned to caller}}
156   }();
157 }