https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67872
--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> --- Looking for existing code in place to issue -Warray-bounds warnings I came across fold_offsetof_1 in c-family/c-common.c. The function is designed to warn for out of bounds indices in offsetof expressions but doesn't detect the following: struct A { int a[3]; } a; int foo (void) { return __builtin_offsetof (struct A, a[4]); } This (otherwise untested) patch fixes it and makes the function diagnose this case. (The comment about flexible array members above the block suggests that the patch might need tweaking to avoid false positives for such constructs.) --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -10623,7 +10623,8 @@ fold_offsetof_1 (tree expr) man's flexible array member with a very permissive definition thereof. */ if (TREE_CODE (v) == ARRAY_REF - || TREE_CODE (v) == COMPONENT_REF) + || TREE_CODE (v) == COMPONENT_REF + || TREE_CODE (v) == INDIRECT_REF) warning (OPT_Warray_bounds, "index %E denotes an offset " "greater than size of %qT",