blob: 5f402eee35dd5adce71a8753b70d6d0ad02f00af [file] [log] [blame]
Ted Kremenekeea8c292009-07-06 21:58:46 +00001// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -verify %s &&
Ted Kremenekf6655792009-07-10 00:41:58 +00002// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic-old-cast -verify %s &&
Daniel Dunbara45cf5b2009-03-24 02:24:46 +00003// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -verify %s
Ted Kremenek6e06f912008-04-24 18:28:14 +00004
5struct FPRec {
6 void (*my_func)(int * x);
7};
8
9int bar(int x);
10
11int f1_a(struct FPRec* foo) {
12 int x;
13 (*foo->my_func)(&x);
14 return bar(x)+1; // no-warning
15}
16
17int f1_b() {
18 int x;
Douglas Gregor5741efb2009-03-01 17:12:46 +000019 return bar(x)+1; // expected-warning{{Pass-by-value argument in function call is undefined.}}
Ted Kremenek6e06f912008-04-24 18:28:14 +000020}
Ted Kremenekdb04a9e2008-05-05 15:56:53 +000021
22int f2() {
23
24 int x;
25
26 if (x+1) // expected-warning{{Branch}}
27 return 1;
28
29 return 2;
30}
31
32int f2_b() {
33 int x;
34
35 return ((x+1)+2+((x))) + 1 ? 1 : 2; // expected-warning{{Branch}}
36}
37
Ted Kremenekb120ff12008-05-21 15:48:33 +000038int f3(void) {
39 int i;
40 int *p = &i;
41 if (*p > 0) // expected-warning{{Branch condition evaluates to an uninitialized value}}
42 return 0;
43 else
44 return 1;
45}
Zhongxing Xu16a92af2008-11-19 11:10:42 +000046
Ted Kremenek701fc102009-04-02 17:25:00 +000047void f4_aux(float* x);
48float f4(void) {
49 float x;
50 f4_aux(&x);
51 return x; // no-warning
52}
53
54struct f5_struct { int x; };
55void f5_aux(struct f5_struct* s);
56int f5(void) {
57 struct f5_struct s;
58 f5_aux(&s);
59 return s.x; // no-warning
60}
61
Ted Kremenekdfcbcfe2008-11-21 00:28:47 +000062int ret_uninit() {
63 int i;
64 int *p = &i;
Ted Kremenekbae77722009-05-13 19:16:35 +000065 return *p; // expected-warning{{Uninitialized or undefined value returned to caller.}}
Ted Kremenekdfcbcfe2008-11-21 00:28:47 +000066}
67
Ted Kremenek300c9cc2008-12-17 19:42:34 +000068// <rdar://problem/6451816>
69typedef unsigned char Boolean;
70typedef const struct __CFNumber * CFNumberRef;
71typedef signed long CFIndex;
72typedef CFIndex CFNumberType;
73typedef unsigned long UInt32;
74typedef UInt32 CFStringEncoding;
75typedef const struct __CFString * CFStringRef;
76extern Boolean CFNumberGetValue(CFNumberRef number, CFNumberType theType, void *valuePtr);
77extern CFStringRef CFStringConvertEncodingToIANACharSetName(CFStringEncoding encoding);
78
79CFStringRef rdar_6451816(CFNumberRef nr) {
80 CFStringEncoding encoding;
81 // &encoding is casted to void*. This test case tests whether or not
82 // we properly invalidate the value of 'encoding'.
83 CFNumberGetValue(nr, 9, &encoding);
84 return CFStringConvertEncodingToIANACharSetName(encoding); // no-warning
85}
Ted Kremenekdfcbcfe2008-11-21 00:28:47 +000086
Ted Kremenekfaf0c642009-07-28 19:24:31 +000087// PR 4630 - false warning with nonnull attribute
88// This false positive (due to a regression) caused the analyzer to falsely
89// flag a "return of uninitialized value" warning in the first branch due to
90// the nonnull attribute.
91void pr_4630_aux(char *x, int *y) __attribute__ ((nonnull (1)));
92void pr_4630_aux_2(char *x, int *y);
93int pr_4630(char *a, int y) {
94 int x;
95 if (y) {
96 pr_4630_aux(a, &x);
97 return x; // no-warning
98 }
99 else {
100 pr_4630_aux_2(a, &x);
101 return x; // no-warning
102 }
103}
104
Ted Kremeneka41d9dd2009-07-28 20:46:55 +0000105// PR 4631 - False positive with union initializer
106// Previously the analyzer didn't examine the compound initializers of unions,
107// resulting in some false positives for initializers with side-effects.
108union u_4631 { int a; };
109struct s_4631 { int a; };
110int pr4631_f2(int *p);
111int pr4631_f3(void *q);
112int pr4631_f1(void)
113{
114 int x;
115 union u_4631 m = { pr4631_f2(&x) };
116 pr4631_f3(&m); // tell analyzer that we use m
117 return x; // no-warning
118}
119int pr4631_f1_b(void)
120{
121 int x;
122 struct s_4631 m = { pr4631_f2(&x) };
123 pr4631_f3(&m); // tell analyzer that we use m
124 return x; // no-warning
125}
126