https://gcc.gnu.org/g:120c810c1e94e83f53fc2891f5a5f138927c2235
commit r17-6648-g120c810c1e94e83f53fc2891f5a5f138927c2235 Author: Torbjörn SVENSSON <[email protected]> Date: Sun Nov 22 20:32:39 2026 +0100 analyzer: use original signedness for tainted offset checks Since r17-6246-g7647c00b77ee1a, a cast between integer types of the same width, like "unsigned int" to "sizetype" on ILP32 targets, is no longer an undoable cast. The taint check on offsets therefore loses the original unsigned type and its implicit lower bound of 0. It then said "without bounds checking" where it should say "without upper-bounds checking". It also warned on offsets that had already been checked against an upper bound. Recover the original type from the representative tree and use it when checking the offset for taint. This fixes these failures on ILP32 targets: FAIL: gcc.dg/analyzer/taint-write-offset-1.c (test for warnings, line 100) FAIL: gcc.dg/analyzer/taint-write-offset-1.c (test for warnings, line 89) FAIL: gcc.dg/analyzer/taint-write-offset-1.c (test for excess errors) It also restores the original expected results in taint-read-offset-1.c. That test uses size_t, so it was affected on all targets. gcc/analyzer/ChangeLog: * sm-taint.cc (region_model::check_region_for_taint): Use the type of the representative tree for same-width casts of the offset. gcc/testsuite/ChangeLog: * gcc.dg/analyzer/taint-read-offset-1.c (test_6, test_7): Expect "upper-bounds" in the warning again. (test_8): Remove xfail. Signed-off-by: Torbjörn SVENSSON <[email protected]> Diff: --- gcc/analyzer/sm-taint.cc | 28 +++++++++++++++++----- .../gcc.dg/analyzer/taint-read-offset-1.c | 6 ++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/gcc/analyzer/sm-taint.cc b/gcc/analyzer/sm-taint.cc index 36d1dfa4d880..a3ac28ec9d64 100644 --- a/gcc/analyzer/sm-taint.cc +++ b/gcc/analyzer/sm-taint.cc @@ -1762,16 +1762,32 @@ region_model::check_region_for_taint (const region *reg, gcc_assert (state); /* Handle implicit cast to sizetype. */ tree effective_type = offset->get_type (); + const svalue *effective_offset = offset; if (const svalue *cast = offset->maybe_undo_cast ()) - if (cast->get_type ()) - effective_type = cast->get_type (); + { + effective_offset = cast; + if (cast->get_type ()) + effective_type = cast->get_type (); + } + /* Same-width casts can't be undone; recover the original + type from the representative tree. */ + tree arg = NULL_TREE; + if (effective_offset->dyn_cast_svalue_from_bit_vector ()) + { + arg = get_representative_tree (effective_offset); + if (arg + && INTEGRAL_TYPE_P (TREE_TYPE (arg)) + && effective_type + && INTEGRAL_TYPE_P (effective_type) + && (TYPE_PRECISION (TREE_TYPE (arg)) + == TYPE_PRECISION (effective_type))) + effective_type = TREE_TYPE (arg); + } enum bounds b; if (taint_sm.get_taint (state, effective_type, &b)) { - const svalue *effective_offset = offset; - if (const svalue *cast = offset->maybe_undo_cast ()) - effective_offset = cast; - tree arg = get_representative_tree (effective_offset); + if (!arg) + arg = get_representative_tree (effective_offset); ctxt->warn (std::make_unique<tainted_offset> (taint_sm, arg, b, offset)); } diff --git a/gcc/testsuite/gcc.dg/analyzer/taint-read-offset-1.c b/gcc/testsuite/gcc.dg/analyzer/taint-read-offset-1.c index 700387897d33..2f2a23ac540f 100644 --- a/gcc/testsuite/gcc.dg/analyzer/taint-read-offset-1.c +++ b/gcc/testsuite/gcc.dg/analyzer/taint-read-offset-1.c @@ -83,7 +83,7 @@ char test_6(FILE *f) struct bar tmp; if (1 == fread(&tmp, sizeof(tmp), 1, f)) { - return *(p + tmp.offset); /* { dg-warning "use of attacker-controlled value 'tmp.offset' as offset without bounds checking" } */ + return *(p + tmp.offset); /* { dg-warning "use of attacker-controlled value 'tmp.offset' as offset without upper-bounds checking" } */ } return 0; } @@ -94,7 +94,7 @@ char test_7(FILE *f) if (1 == fread(&tmp, sizeof(tmp), 1, f)) { if (tmp.offset >= 0) { - return *(p + tmp.offset); /* { dg-warning "use of attacker-controlled value 'tmp.offset' as offset without bounds checking" } */ + return *(p + tmp.offset); /* { dg-warning "use of attacker-controlled value 'tmp.offset' as offset without upper-bounds checking" } */ } } return 0; @@ -107,7 +107,7 @@ char test_8(FILE *f) if (1 == fread(&tmp, sizeof(tmp), 1, f)) { if (tmp.offset < 256) { /* not a bug: has an upper bound, and an implicit lower bound: */ - return *(p + tmp.offset); /* { dg-bogus "attacker-controlled" "TODO(file PR)" { xfail *-*-* } } */ + return *(p + tmp.offset); /* { dg-bogus "attacker-controlled" } */ } } return 0;
