xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/uninit-const.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=cplusplus.NewDelete,core,alpha.core.CallAndMessageUnInitRefArg -analyzer-output=text -verify %s
2*0a6a1f1dSLionel Sambuc // Passing uninitialized const data to unknown function
3*0a6a1f1dSLionel Sambuc 
4*0a6a1f1dSLionel Sambuc #include "Inputs/system-header-simulator-cxx.h"
5*0a6a1f1dSLionel Sambuc 
6*0a6a1f1dSLionel Sambuc void doStuff6(const int& c);
7*0a6a1f1dSLionel Sambuc void doStuff4(const int y);
8*0a6a1f1dSLionel Sambuc void doStuff3(int& g);
9*0a6a1f1dSLionel Sambuc void doStuff_uninit(const int *u);
10*0a6a1f1dSLionel Sambuc 
11*0a6a1f1dSLionel Sambuc 
f10(void)12*0a6a1f1dSLionel Sambuc int f10(void) {
13*0a6a1f1dSLionel Sambuc   int *ptr;
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc   ptr = new int; //
16*0a6a1f1dSLionel Sambuc   if(*ptr) {
17*0a6a1f1dSLionel Sambuc     doStuff4(*ptr);
18*0a6a1f1dSLionel Sambuc   }
19*0a6a1f1dSLionel Sambuc   delete ptr;
20*0a6a1f1dSLionel Sambuc   return 0;
21*0a6a1f1dSLionel Sambuc }
22*0a6a1f1dSLionel Sambuc 
f9(void)23*0a6a1f1dSLionel Sambuc int f9(void) {
24*0a6a1f1dSLionel Sambuc   int *ptr;
25*0a6a1f1dSLionel Sambuc 
26*0a6a1f1dSLionel Sambuc   ptr = new int; //
27*0a6a1f1dSLionel Sambuc 
28*0a6a1f1dSLionel Sambuc   doStuff_uninit(ptr); // no warning
29*0a6a1f1dSLionel Sambuc   delete ptr;
30*0a6a1f1dSLionel Sambuc   return 0;
31*0a6a1f1dSLionel Sambuc }
32*0a6a1f1dSLionel Sambuc 
f8(void)33*0a6a1f1dSLionel Sambuc int f8(void) {
34*0a6a1f1dSLionel Sambuc   int *ptr;
35*0a6a1f1dSLionel Sambuc 
36*0a6a1f1dSLionel Sambuc   ptr = new int;
37*0a6a1f1dSLionel Sambuc   *ptr = 25;
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc   doStuff_uninit(ptr); // no warning?
40*0a6a1f1dSLionel Sambuc   delete ptr;
41*0a6a1f1dSLionel Sambuc   return 0;
42*0a6a1f1dSLionel Sambuc }
43*0a6a1f1dSLionel Sambuc 
f7(void)44*0a6a1f1dSLionel Sambuc void f7(void) {
45*0a6a1f1dSLionel Sambuc   int m = 3;
46*0a6a1f1dSLionel Sambuc   doStuff6(m); // no warning
47*0a6a1f1dSLionel Sambuc }
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc 
f6_1_sub(int & p)50*0a6a1f1dSLionel Sambuc int& f6_1_sub(int &p) {
51*0a6a1f1dSLionel Sambuc   return p;
52*0a6a1f1dSLionel Sambuc }
53*0a6a1f1dSLionel Sambuc 
f6_1(void)54*0a6a1f1dSLionel Sambuc void f6_1(void) {
55*0a6a1f1dSLionel Sambuc   int t;
56*0a6a1f1dSLionel Sambuc   int p = f6_1_sub(t); //expected-warning {{Assigned value is garbage or undefined}}
57*0a6a1f1dSLionel Sambuc                        //expected-note@-1 {{Calling 'f6_1_sub'}}
58*0a6a1f1dSLionel Sambuc                        //expected-note@-2 {{Returning from 'f6_1_sub'}}
59*0a6a1f1dSLionel Sambuc                        //expected-note@-3 {{Assigned value is garbage or undefined}}
60*0a6a1f1dSLionel Sambuc   int q = p;
61*0a6a1f1dSLionel Sambuc   doStuff6(q);
62*0a6a1f1dSLionel Sambuc }
63*0a6a1f1dSLionel Sambuc 
f6_2(void)64*0a6a1f1dSLionel Sambuc void f6_2(void) {
65*0a6a1f1dSLionel Sambuc   int t;       //expected-note {{'t' declared without an initial value}}
66*0a6a1f1dSLionel Sambuc   int &p = t;
67*0a6a1f1dSLionel Sambuc   int &s = p;
68*0a6a1f1dSLionel Sambuc   int &q = s;  //expected-note {{'q' initialized here}}
69*0a6a1f1dSLionel Sambuc   doStuff6(q); //expected-warning {{Function call argument is an uninitialized value}}
70*0a6a1f1dSLionel Sambuc                //expected-note@-1 {{Function call argument is an uninitialized value}}
71*0a6a1f1dSLionel Sambuc }
72*0a6a1f1dSLionel Sambuc 
doStuff6_3(int & q_,int * ptr_)73*0a6a1f1dSLionel Sambuc void doStuff6_3(int& q_, int *ptr_) {}
74*0a6a1f1dSLionel Sambuc 
f6_3(void)75*0a6a1f1dSLionel Sambuc void f6_3(void) {
76*0a6a1f1dSLionel Sambuc   int *ptr;    //expected-note {{'ptr' declared without an initial value}}
77*0a6a1f1dSLionel Sambuc   int t;
78*0a6a1f1dSLionel Sambuc   int &p = t;
79*0a6a1f1dSLionel Sambuc   int &s = p;
80*0a6a1f1dSLionel Sambuc   int &q = s;
81*0a6a1f1dSLionel Sambuc   doStuff6_3(q,ptr); //expected-warning {{Function call argument is an uninitialized value}}
82*0a6a1f1dSLionel Sambuc                //expected-note@-1 {{Function call argument is an uninitialized value}}
83*0a6a1f1dSLionel Sambuc 
84*0a6a1f1dSLionel Sambuc }
85*0a6a1f1dSLionel Sambuc 
f6(void)86*0a6a1f1dSLionel Sambuc void f6(void) {
87*0a6a1f1dSLionel Sambuc   int k;       // expected-note {{'k' declared without an initial value}}
88*0a6a1f1dSLionel Sambuc   doStuff6(k); // expected-warning {{Function call argument is an uninitialized value}}
89*0a6a1f1dSLionel Sambuc                // expected-note@-1 {{Function call argument is an uninitialized value}}
90*0a6a1f1dSLionel Sambuc 
91*0a6a1f1dSLionel Sambuc }
92*0a6a1f1dSLionel Sambuc 
93*0a6a1f1dSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc 
f5(void)95*0a6a1f1dSLionel Sambuc void f5(void) {
96*0a6a1f1dSLionel Sambuc   int t;
97*0a6a1f1dSLionel Sambuc   int* tp = &t;        // expected-note {{'tp' initialized here}}
98*0a6a1f1dSLionel Sambuc   doStuff_uninit(tp);  // expected-warning {{Function call argument is a pointer to uninitialized value}}
99*0a6a1f1dSLionel Sambuc                        // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
100*0a6a1f1dSLionel Sambuc }
101*0a6a1f1dSLionel Sambuc 
102*0a6a1f1dSLionel Sambuc 
f4(void)103*0a6a1f1dSLionel Sambuc void f4(void) {
104*0a6a1f1dSLionel Sambuc       int y;        // expected-note {{'y' declared without an initial value}}
105*0a6a1f1dSLionel Sambuc       doStuff4(y);  // expected-warning {{Function call argument is an uninitialized value}}
106*0a6a1f1dSLionel Sambuc                     // expected-note@-1 {{Function call argument is an uninitialized value}}
107*0a6a1f1dSLionel Sambuc }
108*0a6a1f1dSLionel Sambuc 
f3(void)109*0a6a1f1dSLionel Sambuc void f3(void) {
110*0a6a1f1dSLionel Sambuc       int g;
111*0a6a1f1dSLionel Sambuc       doStuff3(g); // no warning
112*0a6a1f1dSLionel Sambuc }
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc int z;
f2(void)115*0a6a1f1dSLionel Sambuc void f2(void) {
116*0a6a1f1dSLionel Sambuc       doStuff_uninit(&z);  // no warning
117*0a6a1f1dSLionel Sambuc }
118*0a6a1f1dSLionel Sambuc 
f1(void)119*0a6a1f1dSLionel Sambuc void f1(void) {
120*0a6a1f1dSLionel Sambuc       int x_=5;
121*0a6a1f1dSLionel Sambuc       doStuff_uninit(&x_);  // no warning
122*0a6a1f1dSLionel Sambuc }
123*0a6a1f1dSLionel Sambuc 
f_uninit(void)124*0a6a1f1dSLionel Sambuc void f_uninit(void) {
125*0a6a1f1dSLionel Sambuc       int x;
126*0a6a1f1dSLionel Sambuc       doStuff_uninit(&x);  // expected-warning {{Function call argument is a pointer to uninitialized value}}
127*0a6a1f1dSLionel Sambuc                            // expected-note@-1 {{Function call argument is a pointer to uninitialized value}}
128*0a6a1f1dSLionel Sambuc }
129