Correct handling of indices into arrays with elements larger than 1 (PR c++/96511)
authorMartin Sebor <msebor@redhat.com>
Fri, 9 Oct 2020 19:56:53 +0000 (13:56 -0600)
committerMartin Sebor <msebor@redhat.com>
Mon, 12 Oct 2020 15:04:49 +0000 (09:04 -0600)
commitde05c19d5fd661ae16dd75a895b49d32d12f5edc
tree05d9906c75f514094c4320b482f200787a34b9b6
parent71dbabccbfb295c87d91719fe72d9d60511c0b44
Correct handling of indices into arrays with elements larger than 1 (PR c++/96511)

Resolves:
PR c++/96511 - Incorrect -Wplacement-new on POINTER_PLUS into an array with 4-byte elements
PR middle-end/96384 - bogus -Wstringop-overflow= storing into multidimensional array with index in range

gcc/ChangeLog:

PR c++/96511
PR middle-end/96384
* builtins.c (get_range): Return full range of type when neither
value nor its range is available.  Fail for ranges inverted due
to the signedness of offsets.
(compute_objsize): Handle more special array members.  Handle
POINTER_PLUS_EXPR and VIEW_CONVERT_EXPR that come up in front end
code.
(access_ref::offset_bounded): Define new member function.
* builtins.h (access_ref::eval): New data member.
(access_ref::offset_bounded): New member function.
(access_ref::offset_zero): New member function.
(compute_objsize): Declare a new overload.
* gimple-array-bounds.cc (array_bounds_checker::check_array_ref): Use
enum special_array_member.
* tree.c (component_ref_size): Use special_array_member.
* tree.h (special_array_member): Define a new type.
(component_ref_size): Change signature.

gcc/cp/ChangeLog:

PR c++/96511
PR middle-end/96384
* init.c (warn_placement_new_too_small): Call builtin_objsize instead
of duplicating what it does.

gcc/testsuite/ChangeLog:

PR c++/96511
PR middle-end/96384
* g++.dg/init/strlen.C: Add expected warning.
* g++.dg/warn/Wplacement-new-size-1.C: Relax warnings.
* g++.dg/warn/Wplacement-new-size-2.C: Same.
* g++.dg/warn/Wplacement-new-size-6.C: Same.
* gcc.dg/Warray-bounds-58.c: Adjust
* gcc.dg/Wstringop-overflow-37.c: Same.
* g++.dg/warn/Wplacement-new-size-7.C: New test.
13 files changed:
gcc/builtins.c
gcc/builtins.h
gcc/cp/init.c
gcc/gimple-array-bounds.cc
gcc/testsuite/g++.dg/init/strlen.C
gcc/testsuite/g++.dg/warn/Wplacement-new-size-1.C
gcc/testsuite/g++.dg/warn/Wplacement-new-size-2.C
gcc/testsuite/g++.dg/warn/Wplacement-new-size-6.C
gcc/testsuite/g++.dg/warn/Wplacement-new-size-7.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/Warray-bounds-58.c
gcc/testsuite/gcc.dg/Wstringop-overflow-37.c
gcc/tree.c
gcc/tree.h