[Bug c/122495] counted_by is not supported correctly for a FAM in union
cvs-commit at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Wed Jan 14 21:06:50 GMT 2026
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122495
--- Comment #1 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Qing Zhao <qinzhao@gcc.gnu.org>:
https://gcc.gnu.org/g:aa27219f9abdd8030fa38aae64c3a4d1231c73e8
commit r16-6784-gaa27219f9abdd8030fa38aae64c3a4d1231c73e8
Author: Qing Zhao <qing.zhao@oracle.com>
Date: Wed Jan 14 17:14:01 2026 +0000
C: fix issues when supporting counted_by fields in anonymous
structure/unions [PR122495,PR122496]
Currently, GCC does not fully support the cases when a FAM or pointer field
and
its corresponding counted_by field are in different anonymous
structure/unions
of a common named structure.
For example:
struct nested_mixed {
struct {
union {
int b;
float f;
};
int n;
};
struct {
PTR_TYPE *pointer __attribute__((__counted_by__(n)));
FAM_TYPE c[] __attribute__((__counted_by__(b)));
};
} *nested_mixed_annotated;
In order to support such cases, we always need to locate the first outer
named structure as the root, and then lookup_field inside this named
structure. When building the component_ref for the counted_by field,
we need to build a chain of component_ref starting from the root structure.
When supporting the above in general, we also need to handle the following
several special cases correctly:
A. Support an untagged type as its own top-level type. */
struct { int a; char b[] __attribute__ ((counted_by (a))); } *x;
B. Support an unnamed field with a named struct/union. */
struct s { struct { int a; char b[] __attribute__ ((counted_by (a))); } *x;
} *y;
C. When -fms-extensions is enabled:
C.1 Do not support the inward-to-outward counted-by field reference
since checking the validity of such reference depends on unknown
situation at the end of the structure definition.
struct bar {
char *buf __counted_by (n); /* { dg-error "attribute is not a field
declaration in the same structure as" } */
};
C.2 support the outward-to-inward counted-by field reference.
PR C/122495
PR C/122496
gcc/c/ChangeLog:
* c-decl.cc (grokfield): Call
verify_counted_by_for_top_anonymous_type
for named field.
(verify_counted_by_attribute): Change the prototype to a recursive
routine.
(verify_counted_by_for_top_anonymous_type): New routine.
(finish_struct): Set C_TYPE_FIELDS_HAS_COUNTED_BY and call the
routine
verify_counted_by_attribute only for named structure.
* c-parser.cc (c_parser_declaration_or_fndef): Call
verify_counted_by_for_top_anonymous_type for the decl.
(c_parser_parameter_declaration): Call
verify_counted_by_for_top_anonymous_type for the parameter.
(c_parser_type_name): Call verify_counted_by_for_top_anonymous_type
for the type.
* c-tree.h (C_TYPE_FIELDS_HAS_COUNTED_BY): New flag.
(verify_counted_by_for_top_anonymous_type): New routine.
* c-typeck.cc (build_counted_by_ref): Locate the root named
structure,
build a chain of component_ref starting from the root structure.
gcc/testsuite/ChangeLog:
* gcc.dg/counted-by-anonymous-2-char.c: New test.
* gcc.dg/counted-by-anonymous-2-float.c: New test.
* gcc.dg/counted-by-anonymous-2-struct.c: New test.
* gcc.dg/counted-by-anonymous-2-union.c: New test.
* gcc.dg/counted-by-anonymous-2.c: New test.
* gcc.dg/counted-by-anonymous-3.c: New test.
* gcc.dg/counted-by-anonymous.c: New test.
* gcc.dg/ubsan/counted-by-anonymous-bounds-1.c: New test.
* gcc.dg/ubsan/counted-by-anonymous-bounds-2.c: New test.
* gcc.dg/ubsan/counted-by-anonymous-bounds.c: New test.
More information about the Gcc-bugs
mailing list