blob: 915961aa11da87f1ef262f7a9f0b135c5351e4fc [file] [log] [blame]
Argyrios Kyrtzidisc4d2c902011-02-28 19:49:42 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store=basic -verify %s
2// RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store=region -verify %s
Ted Kremenek43ae4b02008-04-24 18:28:14 +00003
4struct FPRec {
5 void (*my_func)(int * x);
6};
7
8int bar(int x);
9
10int f1_a(struct FPRec* foo) {
11 int x;
12 (*foo->my_func)(&x);
13 return bar(x)+1; // no-warning
14}
15
16int f1_b() {
17 int x;
Ted Kremenek818b4332010-09-09 22:51:55 +000018 return bar(x)+1; // expected-warning{{Function call argument is an uninitialized value}}
Ted Kremenek43ae4b02008-04-24 18:28:14 +000019}
Ted Kremenek5c454ab2008-05-05 15:56:53 +000020
21int f2() {
22
23 int x;
24
Ted Kremenek112ba7e2009-09-24 00:44:26 +000025 if (x+1) // expected-warning{{The left operand of '+' is a garbage value}}
Ted Kremenek5c454ab2008-05-05 15:56:53 +000026 return 1;
27
28 return 2;
29}
30
31int f2_b() {
32 int x;
33
Ted Kremenek112ba7e2009-09-24 00:44:26 +000034 return ((1+x)+2+((x))) + 1 ? 1 : 2; // expected-warning{{The right operand of '+' is a garbage value}}
Ted Kremenek5c454ab2008-05-05 15:56:53 +000035}
36
Ted Kremenek5c96c272008-05-21 15:48:33 +000037int f3(void) {
38 int i;
39 int *p = &i;
Ted Kremenek112ba7e2009-09-24 00:44:26 +000040 if (*p > 0) // expected-warning{{The left operand of '>' is a garbage value}}
Ted Kremenek5c96c272008-05-21 15:48:33 +000041 return 0;
42 else
43 return 1;
44}
Zhongxing Xu89e8a072008-11-19 11:10:42 +000045
Ted Kremenekef77d542009-04-02 17:25:00 +000046void f4_aux(float* x);
47float f4(void) {
48 float x;
49 f4_aux(&x);
50 return x; // no-warning
51}
52
53struct f5_struct { int x; };
54void f5_aux(struct f5_struct* s);
55int f5(void) {
56 struct f5_struct s;
57 f5_aux(&s);
58 return s.x; // no-warning
59}
60
Ted Kremenek7c686662008-11-21 00:28:47 +000061int ret_uninit() {
62 int i;
63 int *p = &i;
Ted Kremenek5b9bd212009-09-11 22:07:28 +000064 return *p; // expected-warning{{Undefined or garbage value returned to caller}}
Ted Kremenek7c686662008-11-21 00:28:47 +000065}
66
Ted Kremenek90b32362008-12-17 19:42:34 +000067// <rdar://problem/6451816>
68typedef unsigned char Boolean;
69typedef const struct __CFNumber * CFNumberRef;
70typedef signed long CFIndex;
71typedef CFIndex CFNumberType;
72typedef unsigned long UInt32;
73typedef UInt32 CFStringEncoding;
74typedef const struct __CFString * CFStringRef;
75extern Boolean CFNumberGetValue(CFNumberRef number, CFNumberType theType, void *valuePtr);
76extern CFStringRef CFStringConvertEncodingToIANACharSetName(CFStringEncoding encoding);
77
78CFStringRef rdar_6451816(CFNumberRef nr) {
79 CFStringEncoding encoding;
80 // &encoding is casted to void*. This test case tests whether or not
81 // we properly invalidate the value of 'encoding'.
82 CFNumberGetValue(nr, 9, &encoding);
83 return CFStringConvertEncodingToIANACharSetName(encoding); // no-warning
84}
Ted Kremenek7c686662008-11-21 00:28:47 +000085
Ted Kremenekbb977222009-07-28 19:24:31 +000086// PR 4630 - false warning with nonnull attribute
87// This false positive (due to a regression) caused the analyzer to falsely
88// flag a "return of uninitialized value" warning in the first branch due to
89// the nonnull attribute.
90void pr_4630_aux(char *x, int *y) __attribute__ ((nonnull (1)));
91void pr_4630_aux_2(char *x, int *y);
92int pr_4630(char *a, int y) {
93 int x;
94 if (y) {
95 pr_4630_aux(a, &x);
96 return x; // no-warning
97 }
98 else {
99 pr_4630_aux_2(a, &x);
100 return x; // no-warning
101 }
102}
103
Ted Kremenekf3bfa212009-07-28 20:46:55 +0000104// PR 4631 - False positive with union initializer
105// Previously the analyzer didn't examine the compound initializers of unions,
106// resulting in some false positives for initializers with side-effects.
107union u_4631 { int a; };
108struct s_4631 { int a; };
109int pr4631_f2(int *p);
110int pr4631_f3(void *q);
111int pr4631_f1(void)
112{
113 int x;
114 union u_4631 m = { pr4631_f2(&x) };
115 pr4631_f3(&m); // tell analyzer that we use m
116 return x; // no-warning
117}
118int pr4631_f1_b(void)
119{
120 int x;
121 struct s_4631 m = { pr4631_f2(&x) };
122 pr4631_f3(&m); // tell analyzer that we use m
123 return x; // no-warning
124}
125