On Tue, 20 Oct 2015, Martin Sebor wrote:
> I think -Warray-bounds should emit consistent diagnostics for invalid
> array references regardless of the contexts. I.e., given
>
> struct S {
> int A [5][7];
> int x;
> } s;
>
> these should both be diagnosed:
>
> int i = offsetof (struct S, A [0][7]);
>
> int *p = &s.A [0][7];
>
> because they are both undefined and both can lead to surprising
> results when used.
But both are valid. &s.A [0][7] means s.A[0] + 7 (as explicitly specified
in C11, neither the & nor the [] is evaluated in this case, but the []
turns into a +), and s.A[0] is an object of type int[7], which decays to a
pointer to the first element of that array, so adding 7 produces a
just-past-end pointer. It's not valid to dereference that pointer, but
the pointer itself is valid (and subtracting 1 from it produces a pointer
you can dereference).
--
Joseph S. Myers
[email protected]