From 820f0940d7ace1306430a9dcf1bd9577508a7a7e Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Tue, 24 Aug 2021 10:49:11 -0600 Subject: Reset PHI base0 flag if it's clear in any argument [PR101977, ...] Resolves: PR middle-end/101600 - Spurious -Warray-bounds downcasting a polymorphic pointer PR middle-end/101977 - bogus -Warray-bounds on a negative index into a parameter in conditional with null gcc/ChangeLog: PR middle-end/101600 PR middle-end/101977 * gimple-ssa-warn-access.cc (maybe_warn_for_bound): Tighten up the phrasing of a warning. (check_access): Use the remaining size after subtracting any offset rather than the whole object size. * pointer-query.cc (access_ref::get_ref): Clear BASE0 flag if it's clear for any nonnull PHI argument. (compute_objsize): Clear argument. gcc/testsuite/ChangeLog: PR middle-end/101600 PR middle-end/101977 * g++.dg/pr100574.C: Prune out valid warning. * gcc.dg/pr20126.c: Same. * gcc.dg/Wstringop-overread.c: Adjust text of expected warnings. Add new instances. * gcc.dg/warn-strnlen-no-nul.c: Same. * g++.dg/warn/Warray-bounds-26.C: New test. * gcc.dg/Warray-bounds-88.c: New test. --- gcc/gimple-ssa-warn-access.cc | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'gcc/gimple-ssa-warn-access.cc') diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc index 4a2dd9a..5df97a6 100644 --- a/gcc/gimple-ssa-warn-access.cc +++ b/gcc/gimple-ssa-warn-access.cc @@ -704,6 +704,15 @@ maybe_warn_for_bound (opt_code opt, location_t loc, GimpleOrTree exp, tree func, if (opt == OPT_Wstringop_overread) { bool maybe = pad && pad->src.phi (); + if (maybe) + { + /* Issue a "maybe" warning only if the PHI refers to objects + at least one of which has more space remaining than the bound. + Otherwise, if the bound is greater, use the definitive form. */ + offset_int remmax = pad->src.size_remaining (); + if (remmax < wi::to_offset (bndrng[0])) + maybe = false; + } if (tree_int_cst_lt (maxobjsize, bndrng[0])) { @@ -788,6 +797,15 @@ maybe_warn_for_bound (opt_code opt, location_t loc, GimpleOrTree exp, tree func, } bool maybe = pad && pad->dst.phi (); + if (maybe) + { + /* Issue a "maybe" warning only if the PHI refers to objects + at least one of which has more space remaining than the bound. + Otherwise, if the bound is greater, use the definitive form. */ + offset_int remmax = pad->dst.size_remaining (); + if (remmax < wi::to_offset (bndrng[0])) + maybe = false; + } if (tree_int_cst_lt (maxobjsize, bndrng[0])) { if (bndrng[0] == bndrng[1]) @@ -1418,7 +1436,7 @@ check_access (GimpleOrTree exp, tree dstwrite, location_t loc = get_location (exp); tree size = dstsize; if (pad && pad->mode == access_read_only) - size = wide_int_to_tree (sizetype, pad->src.sizrng[1]); + size = wide_int_to_tree (sizetype, pad->src.size_remaining ()); if (range[0] && maxread && tree_fits_uhwi_p (size)) { -- cgit v1.1