[C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)
Joseph S. Myers
joseph@codesourcery.com
Wed Jan 15 21:23:00 GMT 2014
On Wed, 15 Jan 2014, Marek Polacek wrote:
> +/* Return true if T is a pointer to a zero-sized struct/union. */
> +
> +bool
> +pointer_to_zero_sized_aggr_p (tree t)
> +{
> + t = strip_pointer_operator (t);
> + return ((RECORD_OR_UNION_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
> + && TYPE_SIZE (t)
> + && integer_zerop (TYPE_SIZE (t)));
Why have the (RECORD_OR_UNION_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
check at all? It may well be the case that those are the only kinds of
types that can have zero size here, but the principle of this error
applies to anything with zero size so it would seem best not to have that
part of the check at all.
strip_pointer_operator seems wrong here. It recursively removes an
arbitrary number of pointer type derivations - but where the types are
pointer to pointer to zero-size, arithmetic is perfectly valid (so you
should have a test that such cases are still accepted, where this patch
version would have rejected them). I believe this function should return
true if the argument is a pointer (to anything) and after removal of
exactly one level of pointer type derivation, the result has zero size
(constant zero - also add a test that the array case where the size is a
const int initialized to 0 is not, for C, rejected, as those are VLAs in C
terms).
--
Joseph S. Myers
joseph@codesourcery.com
More information about the Gcc-patches
mailing list