uninit-vals-ps.c revision f3bfa21565b8145afe9b4886770257e890b0b68d
1// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -verify %s &&
2// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic-old-cast -verify %s &&
3// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -verify %s
4
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;
19  return bar(x)+1;  // expected-warning{{Pass-by-value argument in function call is undefined.}}
20}
21
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
38int 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}
46
47void 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
62int ret_uninit() {
63  int i;
64  int *p = &i;
65  return *p;  // expected-warning{{Uninitialized or undefined value returned to caller.}}
66}
67
68// <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}
86
87// 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
105// 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
127