From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id 6D90E3857B8B; Wed, 15 Jun 2022 08:59:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6D90E3857B8B MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-10069] tree-optimization/105726 - adjust array bound heuristic X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 76ff0e644f5ebe39325d587a0bc7363ce89dcc9e X-Git-Newrev: d76c15430f74024f1658be55cfcd2ed9f581f1bd Message-Id: <20220615085925.6D90E3857B8B@sourceware.org> Date: Wed, 15 Jun 2022 08:59:25 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Jun 2022 08:59:25 -0000 https://gcc.gnu.org/g:d76c15430f74024f1658be55cfcd2ed9f581f1bd commit r11-10069-gd76c15430f74024f1658be55cfcd2ed9f581f1bd Author: Richard Biener Date: Wed May 25 11:49:03 2022 +0200 tree-optimization/105726 - adjust array bound heuristic There's heuristic to detect ptr[1].a[...] out of bound accesses reasoning that if ptr points to an array of aggregates a trailing incomplete array has to have size zero. The following more thoroughly constrains the cases this applies to avoid false positive diagnostics. 2022-05-25 Richard Biener PR tree-optimization/105726 * gimple-ssa-warn-restrict.c (builtin_memref::set_base_and_offset): Constrain array-of-flexarray case more. * g++.dg/warn/Warray-bounds-27.C: New testcase. (cherry picked from commit e7c482b08076bb299742883c4ffd65b31e33200c) Diff: --- gcc/gimple-ssa-warn-restrict.c | 22 +++++++++++++--------- gcc/testsuite/g++.dg/warn/Warray-bounds-27.C | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/gcc/gimple-ssa-warn-restrict.c b/gcc/gimple-ssa-warn-restrict.c index ad37f20afaa..cfaa2179bb6 100644 --- a/gcc/gimple-ssa-warn-restrict.c +++ b/gcc/gimple-ssa-warn-restrict.c @@ -514,7 +514,6 @@ builtin_memref::set_base_and_offset (tree expr) { tree memrefoff = fold_convert (ptrdiff_type_node, TREE_OPERAND (base, 1)); extend_offset_range (memrefoff); - base = TREE_OPERAND (base, 0); if (refoff != HOST_WIDE_INT_MIN && TREE_CODE (expr) == COMPONENT_REF) @@ -527,14 +526,19 @@ builtin_memref::set_base_and_offset (tree expr) REFOFF is set to s[1].b - (char*)s. */ offset_int off = tree_to_shwi (memrefoff); refoff += off; - } - - if (!integer_zerop (memrefoff)) - /* A non-zero offset into an array of struct with flexible array - members implies that the array is empty because there is no - way to initialize such a member when it belongs to an array. - This must be some sort of a bug. */ - refsize = 0; + + if (!integer_zerop (memrefoff) + && !COMPLETE_TYPE_P (TREE_TYPE (expr)) + && multiple_of_p (sizetype, memrefoff, + TYPE_SIZE_UNIT (TREE_TYPE (base)))) + /* A non-zero offset into an array of struct with flexible array + members implies that the array is empty because there is no + way to initialize such a member when it belongs to an array. + This must be some sort of a bug. */ + refsize = 0; + } + + base = TREE_OPERAND (base, 0); } if (TREE_CODE (ref) == COMPONENT_REF) diff --git a/gcc/testsuite/g++.dg/warn/Warray-bounds-27.C b/gcc/testsuite/g++.dg/warn/Warray-bounds-27.C new file mode 100644 index 00000000000..06ce089c4b0 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Warray-bounds-27.C @@ -0,0 +1,16 @@ +// PR105726 +// { dg-do compile } +// { dg-require-effective-target c++11 } +// { dg-options "-O2 -Warray-bounds" } + +#include +#include + +struct X { + char pad[4]; + std::array mField; +}; + +void encode(char* aBuffer, const X& aMessage) { + strncpy(aBuffer, aMessage.mField.data(), 1); // { dg-bogus "bounds" } +}