http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47832

--- Comment #1 from Nicola Pero <nicola at gcc dot gnu.org> 2011-02-21 10:37:10 
UTC ---
Hi Jakub,

> @interface T
> {
>   struct S *u;
> };
> @end

"struct S *" is a pointer, right ?  So it's always the size of a pointer ?
In that case, I don't see any reason why it shouldn't be possible to use
it as an instance variable - it's a bug in the compiler if this is not
allowed. :-)

I think the new check in GCC 4.6 was supposed to catch the case

struct S
{
  int s;
  unsigned char *t[];
};
@interface T
{
  struct S u;
};
@end
@implementation T
{
};
@end

this shouldn't be allowed.  The reason is easy to understand:

 * the list of instance variables in a class (inside @interface T { ... } @end)
   is compiled into a struct in the end ;-)

 * but, if the class is subclassed, the subclass instance variables are added
   at the end of the superclass's struct

 * so, if the list of instance variables ends with a flexible array member,
   you get in trouble when you subclass the class, because the subclass
instance
   variable struct will have a flexible array member *inside* (not at the end)
   of the struct. ;-)

So, flexible array members should not be allowed as instance variables
anywhere.
This is what GCC 4.6 is trying to prevent.

But, in the testcase you show, the instance variable is not a flexible
array member; it's a pointer to a flexible array member.  You can have
pointers to anything you want as instance variables. ;-)

I hope this helps with the Objective-C side.

Looking at the code, the check in encode_array() is not good enough.  When the
instance variable type is encoded, the compiler will follow the pointer and
encode a description of the struct.  The check in encode_array() will then
abort because the struct contains a flexible array member, without realizing
it is part of the struct pointed to.

I guess the fix should remove the check from encode_array() and move it higher
up when instance variables are added.

I can do the fix myself tonight (ie, in the next 12/24 hours) if you want.

Thanks

Reply via email to