xref: /aosp_15_r20/external/clang/test/Analysis/uninit-const.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -analyze -analyzer-checker=unix.Malloc,core,alpha.core.CallAndMessageUnInitRefArg -analyzer-output=text -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li // Passing uninitialized const data to function
4*67e74705SXin Li #include "Inputs/system-header-simulator.h"
5*67e74705SXin Li 
6*67e74705SXin Li typedef __typeof(sizeof(int)) size_t;
7*67e74705SXin Li void *malloc(size_t);
8*67e74705SXin Li void *valloc(size_t);
9*67e74705SXin Li void free(void *);
10*67e74705SXin Li 
11*67e74705SXin Li 
doStuff3(const int y)12*67e74705SXin Li void doStuff3(const int y){}
doStuff2(int g)13*67e74705SXin Li void doStuff2(int g){}
doStuff_pointerToConstInt(const int * u)14*67e74705SXin Li void doStuff_pointerToConstInt(const int *u){};
doStuff_arrayOfConstInt(const int a[])15*67e74705SXin Li void doStuff_arrayOfConstInt(const int a[]){};
16*67e74705SXin Li 
doStuff_constPointerToConstInt(int const * const u)17*67e74705SXin Li void doStuff_constPointerToConstInt              (int const * const u){};
doStuff_constPointerToConstPointerToConstInt(int const * const * const u)18*67e74705SXin Li void doStuff_constPointerToConstPointerToConstInt(int const * const * const u){};
doStuff_pointerToConstPointerToConstInt(int const * const * u)19*67e74705SXin Li void doStuff_pointerToConstPointerToConstInt(int const * const * u){};
doStuff_pointerToPointerToConstInt(int const ** u)20*67e74705SXin Li void doStuff_pointerToPointerToConstInt       (int const **u){};
doStuff_constStaticSizedArray(const int a[static10])21*67e74705SXin Li void doStuff_constStaticSizedArray(const int a[static 10]) {}
doStuff_variadic(const int * u,...)22*67e74705SXin Li void doStuff_variadic(const int *u, ...){};
23*67e74705SXin Li 
f_1(void)24*67e74705SXin Li void f_1(void) {
25*67e74705SXin Li   int t;
26*67e74705SXin Li   int* tp = &t;        // expected-note {{'tp' initialized here}}
27*67e74705SXin Li   doStuff_pointerToConstInt(tp);  // expected-warning {{Function call argument is a pointer to uninitialized value}}
28*67e74705SXin Li                        // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
29*67e74705SXin Li }
30*67e74705SXin Li 
f_1_1(void)31*67e74705SXin Li void f_1_1(void) {
32*67e74705SXin Li   int t;
33*67e74705SXin Li   int* tp1 = &t;
34*67e74705SXin Li   int* tp2 = tp1;        // expected-note {{'tp2' initialized here}}
35*67e74705SXin Li   doStuff_pointerToConstInt(tp2);  // expected-warning {{Function call argument is a pointer to uninitialized value}}
36*67e74705SXin Li                        // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
37*67e74705SXin Li }
38*67e74705SXin Li 
39*67e74705SXin Li 
f_2_sub(int * p)40*67e74705SXin Li int *f_2_sub(int *p) {
41*67e74705SXin Li   return p;
42*67e74705SXin Li }
43*67e74705SXin Li 
f_2(void)44*67e74705SXin Li void f_2(void) {
45*67e74705SXin Li   int t;
46*67e74705SXin Li   int* p = f_2_sub(&t);
47*67e74705SXin Li   int* tp = p; // expected-note {{'tp' initialized here}}
48*67e74705SXin Li   doStuff_pointerToConstInt(tp); // expected-warning {{Function call argument is a pointer to uninitialized value}}
49*67e74705SXin Li                       // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
50*67e74705SXin Li }
51*67e74705SXin Li 
52*67e74705SXin Li int z;
f_3(void)53*67e74705SXin Li void f_3(void) {
54*67e74705SXin Li       doStuff_pointerToConstInt(&z);  // no warning
55*67e74705SXin Li }
56*67e74705SXin Li 
f_4(void)57*67e74705SXin Li void f_4(void) {
58*67e74705SXin Li       int x=5;
59*67e74705SXin Li       doStuff_pointerToConstInt(&x);  // no warning
60*67e74705SXin Li }
61*67e74705SXin Li 
f_5(void)62*67e74705SXin Li void f_5(void) {
63*67e74705SXin Li   int ta[5];
64*67e74705SXin Li   int* tp = ta;        // expected-note {{'tp' initialized here}}
65*67e74705SXin Li   doStuff_pointerToConstInt(tp);  // expected-warning {{Function call argument is a pointer to uninitialized value}}
66*67e74705SXin Li                        // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
67*67e74705SXin Li }
68*67e74705SXin Li 
f_5_1(void)69*67e74705SXin Li void f_5_1(void) {
70*67e74705SXin Li   int ta[5];        // expected-note {{'ta' initialized here}}
71*67e74705SXin Li   doStuff_pointerToConstInt(ta);  // expected-warning {{Function call argument is a pointer to uninitialized value}}
72*67e74705SXin Li                        // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
73*67e74705SXin Li }
74*67e74705SXin Li 
f_6(void)75*67e74705SXin Li void f_6(void) {
76*67e74705SXin Li   int ta[5] = {1,2,3,4,5};
77*67e74705SXin Li   int* tp = ta;
78*67e74705SXin Li   doStuff_pointerToConstInt(tp); // no-warning
79*67e74705SXin Li }
80*67e74705SXin Li 
f_6_1(void)81*67e74705SXin Li void f_6_1(void) {
82*67e74705SXin Li   int ta[5] = {1,2,3,4,5};
83*67e74705SXin Li   doStuff_pointerToConstInt(ta); // no-warning
84*67e74705SXin Li }
85*67e74705SXin Li 
f_7(void)86*67e74705SXin Li void f_7(void) {
87*67e74705SXin Li       int z;        // expected-note {{'z' declared without an initial value}}
88*67e74705SXin Li       int y=z;      // expected-warning {{Assigned value is garbage or undefined}}
89*67e74705SXin Li                     // expected-note@-1 {{Assigned value is garbage or undefined}}
90*67e74705SXin Li       doStuff3(y);
91*67e74705SXin Li }
92*67e74705SXin Li 
f_8(void)93*67e74705SXin Li void f_8(void) {
94*67e74705SXin Li       int g;       // expected-note {{'g' declared without an initial value}}
95*67e74705SXin Li       doStuff2(g); // expected-warning {{Function call argument is an uninitialized value}}
96*67e74705SXin Li                    // expected-note@-1 {{Function call argument is an uninitialized value}}
97*67e74705SXin Li }
98*67e74705SXin Li 
f_9(void)99*67e74705SXin Li void f_9(void) {
100*67e74705SXin Li   int  a[6];
101*67e74705SXin Li   int const *ptau = a;             // expected-note {{'ptau' initialized here}}
102*67e74705SXin Li   doStuff_arrayOfConstInt(ptau);    // expected-warning {{Function call argument is a pointer to uninitialized value}}
103*67e74705SXin Li                                    // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
104*67e74705SXin Li }
105*67e74705SXin Li 
f_10(void)106*67e74705SXin Li void f_10(void) {
107*67e74705SXin Li   int  a[6];                     // expected-note {{'a' initialized here}}
108*67e74705SXin Li   doStuff_arrayOfConstInt(a);    // expected-warning {{Function call argument is a pointer to uninitialized value}}
109*67e74705SXin Li                                  // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
110*67e74705SXin Li }
111*67e74705SXin Li 
f_11(void)112*67e74705SXin Li void f_11(void) {
113*67e74705SXin Li   int t[10];                    //expected-note {{'t' initialized here}}
114*67e74705SXin Li   doStuff_constStaticSizedArray(t);  // expected-warning {{Function call argument is a pointer to uninitialized value}}
115*67e74705SXin Li                                 // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
116*67e74705SXin Li }
117*67e74705SXin Li 
f_12(void)118*67e74705SXin Li void f_12(void) {
119*67e74705SXin Li   int t[10] = {0,1,2,3,4,5,6,7,8,9};
120*67e74705SXin Li   doStuff_constStaticSizedArray(t);  // no-warning
121*67e74705SXin Li 
122*67e74705SXin Li }
123*67e74705SXin Li 
f_malloc_1(void)124*67e74705SXin Li int f_malloc_1(void) {
125*67e74705SXin Li   int *ptr;
126*67e74705SXin Li 
127*67e74705SXin Li   ptr = (int *)malloc(sizeof(int)); // expected-note {{Value assigned to 'ptr'}}
128*67e74705SXin Li 
129*67e74705SXin Li   doStuff_pointerToConstInt(ptr); // expected-warning {{Function call argument is a pointer to uninitialized value}}
130*67e74705SXin Li                        // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
131*67e74705SXin Li   free(ptr);
132*67e74705SXin Li   return 0;
133*67e74705SXin Li }
134*67e74705SXin Li 
f_malloc_2(void)135*67e74705SXin Li int f_malloc_2(void) {
136*67e74705SXin Li   int *ptr;
137*67e74705SXin Li 
138*67e74705SXin Li   ptr = (int *)malloc(sizeof(int));
139*67e74705SXin Li   *ptr = 25;
140*67e74705SXin Li 
141*67e74705SXin Li   doStuff_pointerToConstInt(ptr); // no warning
142*67e74705SXin Li   free(ptr);
143*67e74705SXin Li   return 0;
144*67e74705SXin Li }
145*67e74705SXin Li 
146*67e74705SXin Li // uninit pointer, uninit val
f_variadic_unp_unv(void)147*67e74705SXin Li void f_variadic_unp_unv(void) {
148*67e74705SXin Li   int t;
149*67e74705SXin Li   int v;
150*67e74705SXin Li   int* tp = &t;           // expected-note {{'tp' initialized here}}
151*67e74705SXin Li   doStuff_variadic(tp,v);  // expected-warning {{Function call argument is a pointer to uninitialized value}}
152*67e74705SXin Li                           // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
153*67e74705SXin Li }
154*67e74705SXin Li // uninit pointer, init val
f_variadic_unp_inv(void)155*67e74705SXin Li void f_variadic_unp_inv(void) {
156*67e74705SXin Li   int t;
157*67e74705SXin Li   int v = 3;
158*67e74705SXin Li   int* tp = &t;           // expected-note {{'tp' initialized here}}
159*67e74705SXin Li   doStuff_variadic(tp,v);  // expected-warning {{Function call argument is a pointer to uninitialized value}}
160*67e74705SXin Li                           // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
161*67e74705SXin Li }
162*67e74705SXin Li 
163*67e74705SXin Li // init pointer, uninit val
f_variadic_inp_unv(void)164*67e74705SXin Li void f_variadic_inp_unv(void) {
165*67e74705SXin Li   int t=5;
166*67e74705SXin Li   int v;                  // expected-note {{'v' declared without an initial value}}
167*67e74705SXin Li   int* tp = &t;
168*67e74705SXin Li   doStuff_variadic(tp,v);// expected-warning {{Function call argument is an uninitialized value}}
169*67e74705SXin Li                           // expected-note@-1 {{Function call argument is an uninitialized value}}
170*67e74705SXin Li }
171*67e74705SXin Li 
172*67e74705SXin Li // init pointer, init val
f_variadic_inp_inv(void)173*67e74705SXin Li void f_variadic_inp_inv(void) {
174*67e74705SXin Li   int t=5;
175*67e74705SXin Li   int v = 3;
176*67e74705SXin Li   int* tp = &t;
177*67e74705SXin Li   doStuff_variadic(tp,v); // no-warning
178*67e74705SXin Li }
179*67e74705SXin Li 
180*67e74705SXin Li // init pointer, init pointer
f_variadic_inp_inp(void)181*67e74705SXin Li void f_variadic_inp_inp(void) {
182*67e74705SXin Li   int t=5;
183*67e74705SXin Li   int u=3;
184*67e74705SXin Li   int *vp = &u ;
185*67e74705SXin Li   int *tp = &t;
186*67e74705SXin Li   doStuff_variadic(tp,vp); // no-warning
187*67e74705SXin Li }
188*67e74705SXin Li 
189*67e74705SXin Li //uninit pointer, init pointer
f_variadic_unp_inp(void)190*67e74705SXin Li void f_variadic_unp_inp(void) {
191*67e74705SXin Li   int t;
192*67e74705SXin Li   int u=3;
193*67e74705SXin Li   int *vp = &u ;
194*67e74705SXin Li   int *tp = &t;             // expected-note {{'tp' initialized here}}
195*67e74705SXin Li   doStuff_variadic(tp,vp); // expected-warning {{Function call argument is a pointer to uninitialized value}}
196*67e74705SXin Li                             // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
197*67e74705SXin Li }
198*67e74705SXin Li 
199*67e74705SXin Li //init pointer, uninit pointer
f_variadic_inp_unp(void)200*67e74705SXin Li void f_variadic_inp_unp(void) {
201*67e74705SXin Li   int t=5;
202*67e74705SXin Li   int u;
203*67e74705SXin Li   int *vp = &u ;
204*67e74705SXin Li   int *tp = &t;
205*67e74705SXin Li   doStuff_variadic(tp,vp); // no-warning
206*67e74705SXin Li }
207*67e74705SXin Li 
208*67e74705SXin Li //uninit pointer, uninit pointer
f_variadic_unp_unp(void)209*67e74705SXin Li void f_variadic_unp_unp(void) {
210*67e74705SXin Li   int t;
211*67e74705SXin Li   int u;
212*67e74705SXin Li   int *vp = &u ;
213*67e74705SXin Li   int *tp = &t;             // expected-note {{'tp' initialized here}}
214*67e74705SXin Li   doStuff_variadic(tp,vp); // expected-warning {{Function call argument is a pointer to uninitialized value}}
215*67e74705SXin Li                             // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
216*67e74705SXin Li }
217