Clang Project

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