> On Dec 4, 2025, at 12:43, Joseph Myers <[email protected]> wrote:
> 
> On Tue, 25 Nov 2025, Qing Zhao wrote:
> 
>> @@ -10001,8 +10014,10 @@ finish_struct (location_t loc, tree t, tree 
>> fieldlist, tree attributes,
>> struct_parse_info->struct_types.safe_push (t);
>>      }
>> 
>> -  if (fields_with_counted_by.length () > 0)
>> -    verify_counted_by_attribute (t, &fields_with_counted_by);
>> +  /* Only when the enclosing struct/union type is not anonymous, do more
>> +     verification on the fields with counted_by attributes.  */
>> +  if (c_type_tag (t) != NULL_TREE && C_TYPE_FIELDS_HAS_COUNTED_BY (t))
>> +    verify_counted_by_attribute (t, t);
> 
> I'm not convinced by this in the cases where a struct or union type with 
> no tag needs to be checked because it can be used for objects on its own 
> rather than only having its members accessed as if they were part of the 
> containing structure or union.

Yes, I think you are right.  We should call “verify_counted_by_attribute" for 
the following cases
even though the c_type_tag==NULL_TREE:

struct { char *p __attribute__ ((counted_by (n))); } x;
struct s { struct { char *p __attribute__ ((counted_by (n))); } *x; };

However, where should we call the “verify_counted_by_attribute” for the above 
cases?

Looks like that inside “finish_struct” is not the correct place to do this 
check since at that time we
don’t know whether this structure will be used on its own. 

Only when we are sure that this anonymous structure is used on its own, we 
should call “verify_counted_by_attribute” for it.

So, where in C FE we should check for this? Any suggestions?

> That is, examples like these should give errors.  If these appear in the 
> testsuite somewhere to demonstrate that they do get diagnosed, could you 
> give details of where such cases are tested and how the verification gets 
> called for them in the compiler?
> 
> struct { char *p __attribute__ ((counted_by (n))); } x;
> struct s { struct { char *p __attribute__ ((counted_by (n))); } *x; };

Yeah, I need to add these testing cases into the patch and issue warning for 
them.

Thanks a lot for your comments and help.

Qing
> 
-- 
Joseph S. Myers
[email protected]


Reply via email to