Dominic Chen | 184c624 | 2017-03-03 18:02:02 +0000 | [diff] [blame] | 1 | // RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus.NewDelete,core,alpha.core.CallAndMessageUnInitRefArg -analyzer-output=text -verify %s |
Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 2 | // Passing uninitialized const data to unknown function |
| 3 | |
| 4 | #include "Inputs/system-header-simulator-cxx.h" |
| 5 | |
| 6 | void doStuff6(const int& c); |
| 7 | void doStuff4(const int y); |
| 8 | void doStuff3(int& g); |
| 9 | void doStuff_uninit(const int *u); |
| 10 | |
| 11 | |
| 12 | int 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 | |
| 23 | int 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 | |
| 33 | int 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 | |
| 44 | void f7(void) { |
| 45 | int m = 3; |
| 46 | doStuff6(m); // no warning |
| 47 | } |
| 48 | |
| 49 | |
| 50 | int& f6_1_sub(int &p) { |
| 51 | return p; |
| 52 | } |
| 53 | |
| 54 | void 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 | |
| 64 | void 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 Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 69 | 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 Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void doStuff6_3(int& q_, int *ptr_) {} |
| 74 | |
| 75 | void 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 Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 81 | 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 Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 83 | |
| 84 | } |
| 85 | |
| 86 | void f6(void) { |
| 87 | int k; // expected-note {{'k' declared without an initial value}} |
Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 88 | 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 Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 90 | |
| 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | void f5(void) { |
| 96 | int t; |
| 97 | int* tp = &t; // expected-note {{'tp' initialized here}} |
Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 98 | 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 Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
| 103 | void f4(void) { |
| 104 | int y; // expected-note {{'y' declared without an initial value}} |
Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 105 | 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 Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void f3(void) { |
| 110 | int g; |
| 111 | doStuff3(g); // no warning |
| 112 | } |
| 113 | |
| 114 | int z; |
| 115 | void f2(void) { |
| 116 | doStuff_uninit(&z); // no warning |
| 117 | } |
| 118 | |
| 119 | void f1(void) { |
| 120 | int x_=5; |
| 121 | doStuff_uninit(&x_); // no warning |
| 122 | } |
| 123 | |
| 124 | void f_uninit(void) { |
Artem Dergachev | 37de888 | 2017-04-24 19:30:33 +0000 | [diff] [blame] | 125 | int x; // expected-note {{'x' declared without an initial value}} |
Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 126 | 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 Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 128 | } |