Skip to content

Commit c3dc63d

Browse files
committed
[analyzer][NFC] Add ArrayBound tests to document casting bug
Add a few security.ArrayBound testcases that document the false positives caused the fact that the analyzer doesn't model a cast from `signed char` to `unsigned char`.
1 parent 7391327 commit c3dc63d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

clang/test/Analysis/out-of-bounds.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,34 @@ char test_comparison_with_extent_symbol(struct incomplete *p) {
194194
return ((char *)p)[-1]; // no-warning
195195
}
196196

197+
int table[256], small_table[128];
198+
int test_cast_to_unsigned(signed char x) {
199+
unsigned char y = x;
200+
if (x >= 0)
201+
return x;
202+
// FIXME: Here the analyzer ignores the signed -> unsigned cast, and manages to
203+
// load a negative value from an unsigned variable. This causes an underflow
204+
// report, which is an ugly false positive.
205+
// The underlying issue is tracked by Github ticket #39492.
206+
return table[y]; // expected-warning {{Out of bound access to memory preceding}}
207+
}
208+
209+
int test_cast_to_unsigned_overflow(signed char x) {
210+
unsigned char y = x;
211+
if (x >= 0)
212+
return x;
213+
// A variant of 'test_cast_to_unsigned' where the correct behavior would be
214+
// an overflow report (because the negative values are cast to `unsigned
215+
// char` values that are too large).
216+
// FIXME: See comment in 'test_cast_to_unsigned'.
217+
return small_table[y]; // expected-warning {{Out of bound access to memory preceding}}
218+
}
219+
220+
int test_negative_offset_with_unsigned_idx(void) {
221+
// An example where the subscript operator uses an unsigned index, but the
222+
// underflow report is still justified. (We should try to keep this if we
223+
// silence false positives like the one in 'test_cast_to_unsigned'.)
224+
int *p = table - 10;
225+
unsigned idx = 2u;
226+
return p[idx]; // expected-warning {{Out of bound access to memory preceding}}
227+
}

0 commit comments

Comments
 (0)