1 // RUN: %clang_analyze_cc1 %s \ 2 // RUN: -analyzer-checker=core \ 3 // RUN: -analyzer-checker=alpha.unix.StdCLibraryFunctions \ 4 // RUN: -analyzer-checker=debug.StdCLibraryFunctionsTester \ 5 // RUN: -analyzer-config alpha.unix.StdCLibraryFunctions:DisplayLoadedSummaries=true \ 6 // RUN: -analyzer-checker=debug.ExprInspection \ 7 // RUN: -analyzer-config eagerly-assume=false \ 8 // RUN: -triple i686-unknown-linux \ 9 // RUN: -verify 10 11 // In this test we verify that each argument constraints are described properly. 12 13 // Check NotNullConstraint violation notes. 14 int __not_null(int *); 15 void test_not_null(int *x) { 16 __not_null(nullptr); // \ 17 // expected-warning{{The 1st argument to '__not_null' is NULL but should not be NULL [}} 18 } 19 20 // Check the BufferSizeConstraint violation notes. 21 using size_t = decltype(sizeof(int)); 22 int __buf_size_arg_constraint_concrete(const void *); // size <= 10 23 int __buf_size_arg_constraint(const void *, size_t); // size <= Arg1 24 int __buf_size_arg_constraint_mul(const void *, size_t, size_t); // size <= Arg1 * Arg2 25 void test_buffer_size(int x) { 26 switch (x) { 27 case 1: { 28 char buf[9]; 29 __buf_size_arg_constraint_concrete(buf); // \ 30 // expected-warning{{The 1st argument to '__buf_size_arg_constraint_concrete' is a buffer with size 9 but should be a buffer with size equal to or greater than 10 [}} 31 break; 32 } 33 case 2: { 34 char buf[3]; 35 __buf_size_arg_constraint(buf, 4); // \ 36 // expected-warning{{The 1st argument to '__buf_size_arg_constraint' is a buffer with size 3 but should be a buffer with size equal to or greater than the value of the 2nd argument (which is 4) [}} 37 break; 38 } 39 case 3: { 40 char buf[3]; 41 __buf_size_arg_constraint_mul(buf, 4, 2); // \ 42 // expected-warning{{The 1st argument to '__buf_size_arg_constraint_mul' is a buffer with size 3 but should be a buffer with size equal to or greater than the value of the 2nd argument (which is 4) times the 3rd argument (which is 2) [}} 43 break; 44 } 45 } 46 } 47 48 // Check the RangeConstraint violation notes. 49 50 int __single_val_0(int); // [0, 0] 51 int __single_val_1(int); // [1, 1] 52 int __range_1_2(int); // [1, 2] 53 int __range_m1_1(int); // [-1, 1] 54 int __range_m2_m1(int); // [-2, -1] 55 int __range_m10_10(int); // [-10, 10] 56 int __range_m1_inf(int); // [-1, inf] 57 int __range_0_inf(int); // [0, inf] 58 int __range_1_inf(int); // [1, inf] 59 int __range_minf_m1(int); // [-inf, -1] 60 int __range_minf_0(int); // [-inf, 0] 61 int __range_minf_1(int); // [-inf, 1] 62 int __range_1_2__4_6(int); // [1, 2], [4, 6] 63 int __range_1_2__4_inf(int); // [1, 2], [4, inf] 64 65 int __single_val_out_0(int); // [0, 0] 66 int __single_val_out_1(int); // [1, 1] 67 int __range_out_1_2(int); // [1, 2] 68 int __range_out_m1_1(int); // [-1, 1] 69 int __range_out_m2_m1(int); // [-2, -1] 70 int __range_out_m10_10(int); // [-10, 10] 71 int __range_out_m1_inf(int); // [-1, inf] 72 int __range_out_0_inf(int); // [0, inf] 73 int __range_out_1_inf(int); // [1, inf] 74 int __range_out_minf_m1(int); // [-inf, -1] 75 int __range_out_minf_0(int); // [-inf, 0] 76 int __range_out_minf_1(int); // [-inf, 1] 77 int __range_out_1_2__4_6(int); // [1, 2], [4, 6] 78 int __range_out_1_2__4_inf(int); // [1, 2], [4, inf] 79 80 int __test_case_range_1_2__4_6(int); 81 82 void test_range_values(int x) { 83 switch (x) { 84 case 0: 85 __single_val_0(1); // expected-warning{{is 1 but should be zero}} 86 break; 87 case 1: 88 __single_val_1(2); // expected-warning{{is 2 but should be 1}} 89 break; 90 case 2: 91 __range_1_2(3); // expected-warning{{is 3 but should be 1 or 2}} 92 break; 93 case 3: 94 __range_m1_1(3); // expected-warning{{is 3 but should be between -1 and 1}} 95 break; 96 case 4: 97 __range_m2_m1(1); // expected-warning{{is 1 but should be -2 or -1}} 98 break; 99 case 5: 100 __range_m10_10(11); // expected-warning{{is 11 but should be between -10 and 10}} 101 break; 102 case 6: 103 __range_m10_10(-11); // expected-warning{{is -11 but should be between -10 and 10}} 104 break; 105 case 7: 106 __range_1_2__4_6(3); // expected-warning{{is 3 but should be 1 or 2 or between 4 and 6}} 107 break; 108 case 8: 109 __range_1_2__4_inf(3); // expected-warning{{is 3 but should be 1 or 2 or >= 4}} 110 break; 111 } 112 } 113 114 void test_range_values_inf(int x) { 115 switch (x) { 116 case 1: 117 __range_m1_inf(-2); // expected-warning{{is -2 but should be >= -1}} 118 break; 119 case 2: 120 __range_0_inf(-1); // expected-warning{{is -1 but should be >= 0}} 121 break; 122 case 3: 123 __range_1_inf(0); // expected-warning{{is 0 but should be > 0}} 124 break; 125 case 4: 126 __range_minf_m1(0); // expected-warning{{is 0 but should be < 0}} 127 break; 128 case 5: 129 __range_minf_0(1); // expected-warning{{is 1 but should be <= 0}} 130 break; 131 case 6: 132 __range_minf_1(2); // expected-warning{{is 2 but should be <= 1}} 133 break; 134 } 135 } 136 137 void test_range_values_out(int x) { 138 switch (x) { 139 case 0: 140 __single_val_out_0(0); // expected-warning{{is 0 but should be nonzero}} 141 break; 142 case 1: 143 __single_val_out_1(1); // expected-warning{{is 1 but should be not equal to 1}} 144 break; 145 case 2: 146 __range_out_1_2(2); // expected-warning{{is 2 but should be not 1 and not 2}} 147 break; 148 case 3: 149 __range_out_m1_1(-1); // expected-warning{{is -1 but should be not between -1 and 1}} 150 break; 151 case 4: 152 __range_out_m2_m1(-2); // expected-warning{{is -2 but should be not -2 and not -1}} 153 break; 154 case 5: 155 __range_out_m10_10(0); // expected-warning{{is 0 but should be not between -10 and 10}} 156 break; 157 case 6: 158 __range_out_1_2__4_6(1); // expected-warning{{is 1 but should be not 1 and not 2 and not between 4 and 6}} 159 break; 160 case 7: 161 __range_out_1_2__4_inf(4); // expected-warning{{is 4 but should be not 1 and not 2 and < 4}} 162 break; 163 } 164 } 165 166 void test_range_values_out_inf(int x) { 167 switch (x) { 168 case 1: 169 __range_out_minf_m1(-1); // expected-warning{{is -1 but should be >= 0}} 170 break; 171 case 2: 172 __range_out_minf_0(0); // expected-warning{{is 0 but should be > 0}} 173 break; 174 case 3: 175 __range_out_minf_1(1); // expected-warning{{is 1 but should be > 1}} 176 break; 177 case 4: 178 __range_out_m1_inf(-1); // expected-warning{{is -1 but should be < -1}} 179 break; 180 case 5: 181 __range_out_0_inf(0); // expected-warning{{is 0 but should be < 0}} 182 break; 183 case 6: 184 __range_out_1_inf(1); // expected-warning{{is 1 but should be <= 0}} 185 break; 186 } 187 } 188 189 void test_explanation(int x, int y) { 190 switch (y) { 191 case 1: 192 if (x > 0) 193 __single_val_0(x); // expected-warning{{is > 0 but should be zero [}} 194 return; 195 case 2: 196 if (x < 0) 197 __single_val_0(x); // expected-warning{{is < 0 but should be zero [}} 198 return; 199 case 3: 200 if (x < -1) 201 __single_val_0(x); // expected-warning{{is < 0 but should be zero [}} 202 return; 203 case 4: 204 if (x != 0) 205 __single_val_0(x); // expected-warning{{is out of the accepted range; It should be zero [}} 206 return; 207 case 5: 208 if (x == 3) 209 __range_1_2__4_6(x); // expected-warning{{is 3 but should be 1 or 2 or between 4 and 6 [}} 210 return; 211 case 6: 212 if (x > 6) 213 __range_1_2__4_6(x); // expected-warning{{is >= 7 but should be 1 or 2 or between 4 and 6 [}} 214 return; 215 case 7: 216 if (x < 1) 217 __range_1_2__4_6(x); // expected-warning{{is <= 0 but should be 1 or 2 or between 4 and 6 [}} 218 return; 219 case 8: 220 if (__test_case_range_1_2__4_6(x) == 1) 221 __range_1_2__4_6(x); // expected-warning{{is 3 or <= 0 but should be 1 or 2 or between 4 and 6 [}} 222 return; 223 case 9: 224 if (__test_case_range_1_2__4_6(x) == 2) 225 __range_1_2__4_6(x); // expected-warning{{is 3 or >= 7 but should be 1 or 2 or between 4 and 6 [}} 226 return; 227 case 10: 228 if (__test_case_range_1_2__4_6(x) == 3) 229 __range_1_2__4_6(x); // expected-warning{{is <= 0 or >= 7 but should be 1 or 2 or between 4 and 6 [}} 230 return; 231 case 11: 232 if (__test_case_range_1_2__4_6(x) == 4) 233 __range_1_2__4_6(x); // expected-warning{{is out of the accepted range; It should be 1 or 2 or between 4 and 6 [}} 234 return; 235 } 236 } 237