This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)
- From: "Joseph S. Myers" <joseph at codesourcery dot com>
- To: Marek Polacek <polacek at redhat dot com>
- Cc: Jakub Jelinek <jakub at redhat dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>, Richard Biener <rguenther at suse dot de>, Jason Merrill <jason at redhat dot com>
- Date: Wed, 15 Jan 2014 21:23:06 +0000
- Subject: Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)
- Authentication-results: sourceware.org; auth=none
- References: <20140113163226 dot GD4458 at redhat dot com> <20140113164859 dot GE4458 at redhat dot com> <20140113204839 dot GF4458 at redhat dot com> <Pine dot LNX dot 4 dot 64 dot 1401142134140 dot 10324 at digraph dot polyomino dot org dot uk> <20140115102737 dot GJ4458 at redhat dot com> <20140115103540 dot GY892 at tucnak dot redhat dot com> <20140115135037 dot GL4458 at redhat dot com>
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