https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62181
--- Comment #9 from Martin Sebor <msebor at gcc dot gnu.org> ---
(In reply to Xi Ruoyao from comment #8)
> (In reply to Xi Ruoyao from comment #7)
>
> > We should make a new PR requesting for clang -Warray-bounds as well. It's
> > a part of meta-bug PR30334.
>
> Sorry. We have -Warray-bounds, but not as well as clang's. For example clang
> warns for "aa"[4], but GCC doesn't.
Right. It only warns on arrays, and only with optimization. That seems to be
because array_at_struct_end_p() in tree.c (called from check_array_ref() in
tree-vrp.c) doesn't handle ARRAY_REF with a STRING_CST operand correctly. It
treats it as if it were a reference to an array at the end of a structure.
This only superficially tested change lets GCC warn on that case as well (as
long as the result is used). To detect this without optimization
-Warray-bounds would need to also do some checking much earlier.
diff --git a/gcc/tree.c b/gcc/tree.c
index 8f87e7c..5fcbf65 100644
--- a/gcc/tree.c
+++ b/gcc/tree.c
@@ -13229,6 +13229,9 @@ array_at_struct_end_p (tree ref, bool allow_compref)
ref = TREE_OPERAND (ref, 0);
}
+ if (TREE_CODE (ref) == STRING_CST)
+ return false;
+
tree size = NULL;
if (TREE_CODE (ref) == MEM_REF