blob: db969bfb67d62994976a7074ab489dfceebe043e [file] [log] [blame]
Dominic Chen184c6242017-03-03 18:02:02 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus.NewDelete,core,alpha.core.CallAndMessageUnInitRefArg -analyzer-output=text -verify %s
Jordan Rose821a3a02014-03-13 17:55:39 +00002// Passing uninitialized const data to unknown function
3
4#include "Inputs/system-header-simulator-cxx.h"
5
6void doStuff6(const int& c);
7void doStuff4(const int y);
8void doStuff3(int& g);
9void doStuff_uninit(const int *u);
10
11
12int f10(void) {
13 int *ptr;
14
15 ptr = new int; //
16 if(*ptr) {
17 doStuff4(*ptr);
18 }
19 delete ptr;
20 return 0;
21}
22
23int f9(void) {
24 int *ptr;
25
26 ptr = new int; //
27
28 doStuff_uninit(ptr); // no warning
29 delete ptr;
30 return 0;
31}
32
33int f8(void) {
34 int *ptr;
35
36 ptr = new int;
37 *ptr = 25;
38
39 doStuff_uninit(ptr); // no warning?
40 delete ptr;
41 return 0;
42}
43
44void f7(void) {
45 int m = 3;
46 doStuff6(m); // no warning
47}
48
49
50int& f6_1_sub(int &p) {
51 return p;
52}
53
54void f6_1(void) {
55 int t;
56 int p = f6_1_sub(t); //expected-warning {{Assigned value is garbage or undefined}}
57 //expected-note@-1 {{Calling 'f6_1_sub'}}
58 //expected-note@-2 {{Returning from 'f6_1_sub'}}
59 //expected-note@-3 {{Assigned value is garbage or undefined}}
60 int q = p;
61 doStuff6(q);
62}
63
64void f6_2(void) {
65 int t; //expected-note {{'t' declared without an initial value}}
66 int &p = t;
67 int &s = p;
68 int &q = s; //expected-note {{'q' initialized here}}
Daniel Marjamaki3d8d6ed2017-03-08 15:22:24 +000069 doStuff6(q); //expected-warning {{1st function call argument is an uninitialized value}}
70 //expected-note@-1 {{1st function call argument is an uninitialized value}}
Jordan Rose821a3a02014-03-13 17:55:39 +000071}
72
73void doStuff6_3(int& q_, int *ptr_) {}
74
75void f6_3(void) {
76 int *ptr; //expected-note {{'ptr' declared without an initial value}}
77 int t;
78 int &p = t;
79 int &s = p;
80 int &q = s;
Daniel Marjamaki3d8d6ed2017-03-08 15:22:24 +000081 doStuff6_3(q,ptr); //expected-warning {{2nd function call argument is an uninitialized value}}
82 //expected-note@-1 {{2nd function call argument is an uninitialized value}}
Jordan Rose821a3a02014-03-13 17:55:39 +000083
84}
85
86void f6(void) {
87 int k; // expected-note {{'k' declared without an initial value}}
Daniel Marjamaki3d8d6ed2017-03-08 15:22:24 +000088 doStuff6(k); // expected-warning {{1st function call argument is an uninitialized value}}
89 // expected-note@-1 {{1st function call argument is an uninitialized value}}
Jordan Rose821a3a02014-03-13 17:55:39 +000090
91}
92
93
94
95void f5(void) {
96 int t;
97 int* tp = &t; // expected-note {{'tp' initialized here}}
Daniel Marjamaki3d8d6ed2017-03-08 15:22:24 +000098 doStuff_uninit(tp); // expected-warning {{1st function call argument is a pointer to uninitialized value}}
99 // expected-note@-1 {{1st function call argument is a pointer to uninitialized value}}
Jordan Rose821a3a02014-03-13 17:55:39 +0000100}
101
102
103void f4(void) {
104 int y; // expected-note {{'y' declared without an initial value}}
Daniel Marjamaki3d8d6ed2017-03-08 15:22:24 +0000105 doStuff4(y); // expected-warning {{1st function call argument is an uninitialized value}}
106 // expected-note@-1 {{1st function call argument is an uninitialized value}}
Jordan Rose821a3a02014-03-13 17:55:39 +0000107}
108
109void f3(void) {
110 int g;
111 doStuff3(g); // no warning
112}
113
114int z;
115void f2(void) {
116 doStuff_uninit(&z); // no warning
117}
118
119void f1(void) {
120 int x_=5;
121 doStuff_uninit(&x_); // no warning
122}
123
124void f_uninit(void) {
Artem Dergachev37de8882017-04-24 19:30:33 +0000125 int x; // expected-note {{'x' declared without an initial value}}
Daniel Marjamaki3d8d6ed2017-03-08 15:22:24 +0000126 doStuff_uninit(&x); // expected-warning {{1st function call argument is a pointer to uninitialized value}}
127 // expected-note@-1 {{1st function call argument is a pointer to uninitialized value}}
Jordan Rose821a3a02014-03-13 17:55:39 +0000128}