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  <rguenther@suse.de>

	PR tree-optimization/105726
	* gimple-ssa-warn-restrict.cc (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 e7c482b080)
This commit is contained in:
Richard Biener 2022-05-25 11:49:03 +02:00
parent cdd93e5b88
commit 9bc27ee76d
2 changed files with 28 additions and 8 deletions

View File

@ -525,7 +525,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)
@ -538,14 +537,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)), true))
/* 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)

View File

@ -0,0 +1,16 @@
// PR105726
// { dg-do compile }
// { dg-require-effective-target c++11 }
// { dg-options "-O2 -Warray-bounds" }
#include <array>
#include <cstring>
struct X {
char pad[4];
std::array<char, 1> mField;
};
void encode(char* aBuffer, const X& aMessage) {
strncpy(aBuffer, aMessage.mField.data(), 1); // { dg-bogus "bounds" }
}