1 // RUN: %clang_analyze_cc1 -triple hexagon-unknown-linux -analyzer-checker=core,valist.Unterminated,valist.CopyToSelf -analyzer-output=text -analyzer-store=region -verify %s
2 // RUN: %clang_analyze_cc1 -triple x86_64-pc-linux-gnu -analyzer-checker=core,valist.Unterminated,valist.CopyToSelf -analyzer-output=text -analyzer-store=region -verify %s
3 
4 #include "Inputs/system-header-simulator-for-valist.h"
5 
f1(int fst,...)6 void f1(int fst, ...) {
7   va_list va;
8   va_start(va, fst); // expected-note{{Initialized va_list}}
9   return; // expected-warning{{Initialized va_list 'va' is leaked}}
10   // expected-note@-1{{Initialized va_list 'va' is leaked}}
11 }
12 
f2(int fst,...)13 void f2(int fst, ...) {
14   va_list va;
15   va_start(va, fst); // expected-note{{Initialized va_list}}
16   va_end(va); // expected-note{{Ended va_list}}
17   va_start(va, fst); // expected-note{{Initialized va_list}}
18 } // expected-warning{{Initialized va_list 'va' is leaked}}
19   // expected-note@-1{{Initialized va_list 'va' is leaked}}
20 
f3(int fst,...)21 void f3(int fst, ...) {
22   va_list va, va2;
23   va_start(va, fst);
24   va_copy(va2, va); // expected-note{{Initialized va_list}}
25   va_end(va); // expected-warning{{Initialized va_list 'va2' is leaked}}
26   // expected-note@-1{{Initialized va_list 'va2' is leaked}}
27 }
28 
f4(va_list * fst,...)29 void f4(va_list *fst, ...) {
30   va_start(*fst, fst); // expected-note{{Initialized va_list}}
31   return; // expected-warning{{Initialized va_list is leaked}}
32   // expected-note@-1{{Initialized va_list is leaked}}
33 }
34 
f5(va_list fst,...)35 void f5(va_list fst, ...) {
36   va_start(fst, fst); // expected-note{{Initialized va_list}}
37 } // expected-warning{{Initialized va_list}}
38   // expected-note@-1{{Initialized va_list}}
39 
f6(va_list * fst,...)40 void f6(va_list *fst, ...) {
41   va_start(*fst, fst); // expected-note{{Initialized va_list}}
42   (void)va_arg(*fst, int);
43   //FIXME: this should NOT cause a warning
44   va_end(*fst); // expected-warning{{Initialized va_list is leaked}}
45   // expected-note@-1{{Initialized va_list is leaked}}
46 }
47 
f7(int * fst,...)48 void f7(int *fst, ...) {
49   va_list x;
50   va_list *y = &x;
51   va_start(*y,fst); // expected-note{{Initialized va_list}}
52 } // expected-warning{{Initialized va_list 'x' is leaked}}
53   // expected-note@-1{{Initialized va_list 'x' is leaked}}
54 
f8(int * fst,...)55 void f8(int *fst, ...) {
56   va_list x;
57   va_list *y = &x;
58   va_start(*y,fst);
59   va_end(x);
60 } // no-warning
61 
reinit(int * fst,...)62 void reinit(int *fst, ...) {
63   va_list va;
64   va_start(va, fst); // expected-note{{Initialized va_list}}
65                      // expected-note@-1{{Initialized va_list}}
66   va_start(va, fst); // expected-warning{{Initialized va_list 'va' is initialized again}}
67   // expected-note@-1{{Initialized va_list 'va' is initialized again}}
68 } // expected-warning{{Initialized va_list 'va' is leaked}}
69   // expected-note@-1{{Initialized va_list 'va' is leaked}}
70 
reinitOk(int * fst,...)71 void reinitOk(int *fst, ...) {
72   va_list va;
73   va_start(va, fst);
74   va_end(va);
75   va_start(va, fst);
76   va_end(va);
77 } // no-warning
78 
copyself(int fst,...)79 void copyself(int fst, ...) {
80   va_list va;
81   va_start(va, fst); // expected-note{{Initialized va_list}}
82   va_copy(va, va); // expected-warning{{va_list 'va' is copied onto itself}}
83                    // expected-note@-1{{va_list 'va' is copied onto itself}}
84   va_end(va);
85 }
86 
copyselfUninit(int fst,...)87 void copyselfUninit(int fst, ...) {
88   va_list va;
89   va_copy(va, va); // expected-warning{{va_list 'va' is copied onto itself}}
90   // expected-note@-1{{va_list 'va' is copied onto itself}}
91 }
92 
copyOverwrite(int fst,...)93 void copyOverwrite(int fst, ...) {
94   va_list va, va2;
95   va_start(va, fst); // expected-note{{Initialized va_list}}
96   va_copy(va, va2); // expected-warning{{Initialized va_list 'va' is overwritten by an uninitialized one}}
97   // expected-note@-1{{Initialized va_list 'va' is overwritten by an uninitialized one}}
98 }
99 
100 //This only generates a warning for the valist.Uninitialized checker
copyUnint(int fst,...)101 void copyUnint(int fst, ...) {
102   va_list va, va2;
103   va_copy(va, va2);
104 } // no-warning
105 
recopy(int fst,...)106 void recopy(int fst, ...) {
107   va_list va, va2;
108   va_start(va, fst);
109   va_copy(va2, va); // expected-note{{Initialized va_list}}
110   va_copy(va2, va); // expected-warning{{Initialized va_list 'va2' is initialized again}}
111   // expected-note@-1{{Initialized va_list 'va2' is initialized again}}
112   va_end(va);
113   va_end(va2);
114 }
115 
doublemsg(int fst,...)116 void doublemsg(int fst, ...) {
117   va_list va, va2;
118   va_start(va, fst), va_start(va2, fst); // expected-warning{{Initialized va_list 'va' is leaked}}
119   // expected-warning@-1{{Initialized va_list 'va2' is leaked}}
120   // expected-note@-2{{Initialized va_list}}
121   // expected-note@-3{{Initialized va_list}}
122   // expected-note@-4{{Initialized va_list}}
123   // expected-note@-5{{Initialized va_list 'va' is leaked}}
124 }
125 
in_array(int fst,...)126 void in_array(int fst, ...) {
127   va_list va_array[8];
128   va_start(va_array[3], fst); // expected-note{{Initialized va_list}}
129 } // expected-warning{{Initialized va_list 'va_array[3]' is leaked}}
130   // expected-note@-1{{Initialized va_list 'va_array[3]' is leaked}}
131 
132 struct containing_a_valist {
133   va_list vafield;
134   int foobar;
135 };
136 
in_struct(int fst,...)137 void in_struct(int fst, ...) {
138   struct containing_a_valist s;
139   va_start(s.vafield, fst); // expected-note{{Initialized va_list}}
140 } // expected-warning{{Initialized va_list 's.vafield' is leaked}}
141   // expected-note@-1{{Initialized va_list 's.vafield' is leaked}}
142 
casting(int fst,...)143 void casting(int fst, ...) {
144   char mem[sizeof(va_list)];
145   va_start(*(va_list *) mem, fst); // expected-note{{Initialized va_list}}
146 } // expected-warning{{Initialized va_list 'mem[0]' is leaked}}
147   // expected-note@-1{{Initialized va_list 'mem[0]' is leaked}}
148 
149 
castingOk(int fst,...)150 void castingOk(int fst, ...) {
151   char mem[sizeof(va_list)];
152   va_start(*(va_list *) mem, fst);
153   va_end(*(va_list *) mem);
154 } // no-warning
155 
156