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