1 // RUN: %clang_analyze_cc1 %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=unix.StdCLibraryFunctions \
4 // RUN: -analyzer-checker=debug.StdCLibraryFunctionsTester \
5 // RUN: -analyzer-config unix.StdCLibraryFunctions:DisplayLoadedSummaries=true \
6 // RUN: -analyzer-checker=debug.ExprInspection \
7 // RUN: -analyzer-config eagerly-assume=false \
8 // RUN: -triple i686-unknown-linux \
11 // In this test we verify that each argument constraints are described properly.
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 [}}
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
) {
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 [}}
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) [}}
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) [}}
48 // Check the RangeConstraint violation notes.
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]
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]
80 int __test_case_range_1_2__4_6(int);
82 void test_range_values(int x
) {
85 __single_val_0(1); // expected-warning{{is 1 but should be zero}}
88 __single_val_1(2); // expected-warning{{is 2 but should be 1}}
91 __range_1_2(3); // expected-warning{{is 3 but should be 1 or 2}}
94 __range_m1_1(3); // expected-warning{{is 3 but should be between -1 and 1}}
97 __range_m2_m1(1); // expected-warning{{is 1 but should be -2 or -1}}
100 __range_m10_10(11); // expected-warning{{is 11 but should be between -10 and 10}}
103 __range_m10_10(-11); // expected-warning{{is -11 but should be between -10 and 10}}
106 __range_1_2__4_6(3); // expected-warning{{is 3 but should be 1 or 2 or between 4 and 6}}
109 __range_1_2__4_inf(3); // expected-warning{{is 3 but should be 1 or 2 or >= 4}}
114 void test_range_values_inf(int x
) {
117 __range_m1_inf(-2); // expected-warning{{is -2 but should be >= -1}}
120 __range_0_inf(-1); // expected-warning{{is -1 but should be >= 0}}
123 __range_1_inf(0); // expected-warning{{is 0 but should be > 0}}
126 __range_minf_m1(0); // expected-warning{{is 0 but should be < 0}}
129 __range_minf_0(1); // expected-warning{{is 1 but should be <= 0}}
132 __range_minf_1(2); // expected-warning{{is 2 but should be <= 1}}
137 void test_range_values_out(int x
) {
140 __single_val_out_0(0); // expected-warning{{is 0 but should be nonzero}}
143 __single_val_out_1(1); // expected-warning{{is 1 but should be not equal to 1}}
146 __range_out_1_2(2); // expected-warning{{is 2 but should be not 1 and not 2}}
149 __range_out_m1_1(-1); // expected-warning{{is -1 but should be not between -1 and 1}}
152 __range_out_m2_m1(-2); // expected-warning{{is -2 but should be not -2 and not -1}}
155 __range_out_m10_10(0); // expected-warning{{is 0 but should be not between -10 and 10}}
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}}
161 __range_out_1_2__4_inf(4); // expected-warning{{is 4 but should be not 1 and not 2 and < 4}}
166 void test_range_values_out_inf(int x
) {
169 __range_out_minf_m1(-1); // expected-warning{{is -1 but should be >= 0}}
172 __range_out_minf_0(0); // expected-warning{{is 0 but should be > 0}}
175 __range_out_minf_1(1); // expected-warning{{is 1 but should be > 1}}
178 __range_out_m1_inf(-1); // expected-warning{{is -1 but should be < -1}}
181 __range_out_0_inf(0); // expected-warning{{is 0 but should be < 0}}
184 __range_out_1_inf(1); // expected-warning{{is 1 but should be <= 0}}
189 void test_explanation(int x
, int y
) {
193 __single_val_0(x
); // expected-warning{{is > 0 but should be zero [}}
197 __single_val_0(x
); // expected-warning{{is < 0 but should be zero [}}
201 __single_val_0(x
); // expected-warning{{is < 0 but should be zero [}}
205 __single_val_0(x
); // expected-warning{{is out of the accepted range; It should be zero [}}
209 __range_1_2__4_6(x
); // expected-warning{{is 3 but should be 1 or 2 or between 4 and 6 [}}
213 __range_1_2__4_6(x
); // expected-warning{{is >= 7 but should be 1 or 2 or between 4 and 6 [}}
217 __range_1_2__4_6(x
); // expected-warning{{is <= 0 but should be 1 or 2 or between 4 and 6 [}}
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 [}}
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 [}}
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 [}}
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 [}}