From 9a27acc30a34b7854db32eac562306cebac6fa1e Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Tue, 26 Oct 2021 14:38:11 -0600 Subject: Make full use of context-sensitive ranges in access warnings. gcc/ChangeLog: * builtins.c (check_strncat_sizes): Pass access_data ctor additional arguments. (expand_builtin_memcmp): Move code to gimple-ssa-warn-access.cc. (expand_builtin_fork_or_exec): Same. * gimple-array-bounds.cc (array_bounds_checker::check_mem_ref): Pass compute_objsize additional arguments. (inbounds_memaccess_p): Same. (array_bounds_checker::check_array_bounds): Add an assert. Stash statement in a member. (check_array_bounds_dom_walker::before_dom_children): Same. * gimple-array-bounds.h (array_bounds_checker::m_stmt): New member. * gimple-ssa-sprintf.c (get_destination_size): Add an argument. (handle_printf_call): Pass a new argument. * gimple-ssa-warn-access.cc (get_size_range): Add an argument. (check_access): Add an argument and pass it along to callees. (check_read_access): Make a member function. (pass_waccess::check_strcat): Pass access_data ctor additional arguments. (pass_waccess::check_strncat): Same. (pass_waccess::check_stxcpy): Same. (pass_waccess::check_stxncpy): Same. (pass_waccess::check_strncmp): Same. (pass_waccess::check_read_access): Same. (pass_waccess::check_builtin): Same. (pass_waccess::maybe_check_access_sizes): Same. (pass_waccess::maybe_check_dealloc_call): Same. * gimple-ssa-warn-access.h (check_read_access): Declare a new member function. * pointer-query.cc (compute_objsize_r): Add an argument. (gimple_call_return_array): Same. (gimple_call_alloc_size): Same. (access_ref::access_ref): Same. (access_ref::get_ref): Same. (pointer_query::get_ref): Same. (handle_min_max_size): Pass an arguments to callees. (handle_array_ref): Add an argument. (handle_mem_ref): Same. (compute_objsize): Same. * pointer-query.h (struct access_ref): Adjust signatures. (struct access_data): Same. (gimple_call_alloc_size): Add an argument. (gimple_parm_array_size): Same. (compute_objsize): Same. * tree-ssa-strlen.c (strlen_pass::adjust_last_stmt): Pass an additional argument to compute_objsize. (strlen_pass::maybe_warn_overflow): Same. (maybe_diag_stxncpy_trunc): Same. gcc/testsuite/ChangeLog: * gcc.dg/Wstringop-overflow-22.c: Correct typos. * gcc.dg/Wstringop-overflow-81.c: New test. libstdc++-v3/ChangeLog: * testsuite/21_strings/basic_string/capacity/1.cc: Also suppress -Wstringop-overread. * testsuite/27_io/filesystem/path/factory/u8path-char8_t.cc: Same. --- gcc/gimple-array-bounds.cc | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'gcc/gimple-array-bounds.cc') diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc index 0517e5d..a353559 100644 --- a/gcc/gimple-array-bounds.cc +++ b/gcc/gimple-array-bounds.cc @@ -426,7 +426,7 @@ array_bounds_checker::check_mem_ref (location_t location, tree ref, axssize = wi::to_offset (access_size); access_ref aref; - if (!compute_objsize (ref, 0, &aref, ranges)) + if (!compute_objsize (ref, m_stmt, 0, &aref, ranges)) return false; if (aref.offset_in_range (axssize)) @@ -667,7 +667,7 @@ array_bounds_checker::check_addr_expr (location_t location, tree t, problems discussed in pr98266 and pr97595. */ static bool -inbounds_memaccess_p (tree t) +inbounds_memaccess_p (tree t, gimple *stmt) { if (TREE_CODE (t) != COMPONENT_REF) return false; @@ -686,7 +686,7 @@ inbounds_memaccess_p (tree t) allocated). */ access_ref aref; // unused tree refop = TREE_OPERAND (mref, 0); - tree refsize = compute_objsize (refop, 1, &aref); + tree refsize = compute_objsize (refop, stmt, 1, &aref); if (!refsize || TREE_CODE (refsize) != INTEGER_CST) return false; @@ -724,6 +724,7 @@ array_bounds_checker::check_array_bounds (tree *tp, int *walk_subtree, { tree t = *tp; struct walk_stmt_info *wi = (struct walk_stmt_info *) data; + location_t location; if (EXPR_HAS_LOCATION (t)) @@ -735,6 +736,8 @@ array_bounds_checker::check_array_bounds (tree *tp, int *walk_subtree, bool warned = false; array_bounds_checker *checker = (array_bounds_checker *) wi->info; + gcc_assert (checker->m_stmt == wi->stmt); + if (TREE_CODE (t) == ARRAY_REF) warned = checker->check_array_ref (location, t, wi->stmt, false/*ignore_off_by_one*/); @@ -746,7 +749,7 @@ array_bounds_checker::check_array_bounds (tree *tp, int *walk_subtree, checker->check_addr_expr (location, t, wi->stmt); *walk_subtree = false; } - else if (inbounds_memaccess_p (t)) + else if (inbounds_memaccess_p (t, wi->stmt)) /* Hack: Skip MEM_REF checks in accesses to a member of a base class at an offset that's within the bounds of the enclosing object. See pr98266 and pr97595. */ @@ -794,14 +797,13 @@ check_array_bounds_dom_walker::before_dom_children (basic_block bb) for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) { gimple *stmt = gsi_stmt (si); - struct walk_stmt_info wi; if (!gimple_has_location (stmt) || is_gimple_debug (stmt)) continue; - memset (&wi, 0, sizeof (wi)); - + struct walk_stmt_info wi{ }; wi.info = checker; + checker->m_stmt = stmt; walk_gimple_op (stmt, array_bounds_checker::check_array_bounds, &wi); } -- cgit v1.1