Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-23 Thread Joseph S. Myers
On Thu, 23 Jan 2014, Marek Polacek wrote: > > 2014-01-16 Marek Polacek > > > > PR c/58346 > > c-family/ > > * c-common.c (pointer_to_zero_sized_aggr_p): New function. > > * c-common.h: Declare it. > > cp/ > > * typeck.c (pointer_diff): Give an error on arithmetic on pointer to

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-23 Thread Marek Polacek
Ping. On Thu, Jan 16, 2014 at 07:52:43PM +0100, Marek Polacek wrote: > On Wed, Jan 15, 2014 at 09:23:06PM +, Joseph S. Myers wrote: > > On Wed, 15 Jan 2014, Marek Polacek wrote: > > > > > +/* Return true if T is a pointer to a zero-sized struct/union. */ > > > + > > > +bool > > > +pointer_to

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-16 Thread Marek Polacek
On Wed, Jan 15, 2014 at 09:23:06PM +, Joseph S. Myers wrote: > 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); > > + ret

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-16 Thread Marek Polacek
On Thu, Jan 16, 2014 at 12:50:10PM +0100, Eric Botcazou 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)

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-16 Thread Eric Botcazou
> +/* 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_S

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-15 Thread Joseph S. Myers
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_

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-15 Thread Marek Polacek
On Wed, Jan 15, 2014 at 11:35:40AM +0100, Jakub Jelinek wrote: > On Wed, Jan 15, 2014 at 11:27:37AM +0100, Marek Polacek wrote: > > Perhaps, but I don't think we can do it easily. Consider > > > > int > > foo (int *p, int *q) > > { > > return p - q; > > } > > That is not a difference of pointe

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-15 Thread Jakub Jelinek
On Wed, Jan 15, 2014 at 11:27:37AM +0100, Marek Polacek wrote: > Perhaps, but I don't think we can do it easily. Consider > > int > foo (int *p, int *q) > { > return p - q; > } That is not a difference of pointers to zero sized arrays though, that is pointers to int, and there is no division b

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-15 Thread Marek Polacek
On Tue, Jan 14, 2014 at 09:42:37PM +, Joseph S. Myers wrote: > On Mon, 13 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); > > + if

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-15 Thread Marek Polacek
On Tue, Jan 14, 2014 at 06:52:00PM +0100, Florian Weimer wrote: > On 01/13/2014 09:48 PM, Marek Polacek wrote: > >+bool > >+pointer_to_zero_sized_aggr_p (tree t) > >+{ > >+ t = strip_pointer_operator (t); > >+ if (RECORD_OR_UNION_TYPE_P (t) > >+ && TYPE_SIZE (t) > >+ && integer_zerop (T

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-14 Thread Joseph S. Myers
On Mon, 13 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); > + if (RECORD_OR_UNION_TYPE_P (t) > + && TYPE_SIZE (t) > + && integer_zerop (TY

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-14 Thread Florian Weimer
On 01/13/2014 09:48 PM, Marek Polacek wrote: +bool +pointer_to_zero_sized_aggr_p (tree t) +{ + t = strip_pointer_operator (t); + if (RECORD_OR_UNION_TYPE_P (t) + && TYPE_SIZE (t) + && integer_zerop (TYPE_SIZE (t))) +return true; + return false; +} I think you can just return th

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-14 Thread Jason Merrill
The C++ part is OK. Jason

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-13 Thread Marek Polacek
On Mon, Jan 13, 2014 at 05:48:59PM +0100, Marek Polacek wrote: > The patch will need some tweaking, I realized that e.g. for struct S { > union {}; }; it doesn't do the right thing... Done in the patch below. CCing Jason for the C++ part. Does this look sane now? Regtested/bootstrapped on x86_6

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-13 Thread Marek Polacek
On Mon, Jan 13, 2014 at 05:32:26PM +0100, Marek Polacek wrote: > This doesn't really fix the PR, but solves a related issue, where we > have e.g. > struct U {}; > static struct U b[6]; > > int foo (struct U *p, struct U *q) > { > return q - p; > } > int main() > { > return foo (&b[0], &b[4]);

Re: [C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-13 Thread Florian Weimer
On 01/13/2014 05:32 PM, Marek Polacek wrote: This doesn't really fix the PR, but solves a related issue, where we have e.g. struct U {}; static struct U b[6]; int foo (struct U *p, struct U *q) { return q - p; } int main() { return foo (&b[0], &b[4]); } Such a program SIGFPEs at runtim

[C PATCH] Disallow subtracting pointers to empty structs (PR c/58346)

2014-01-13 Thread Marek Polacek
This doesn't really fix the PR, but solves a related issue, where we have e.g. struct U {}; static struct U b[6]; int foo (struct U *p, struct U *q) { return q - p; } int main() { return foo (&b[0], &b[4]); } Such a program SIGFPEs at runtime. But subtraction of pointers to empty structures/u