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