https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107952
Bug ID: 107952 Summary: tree-object-size: inconsistent size for flexible arrays nested in structs Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: siddhesh at gcc dot gnu.org Target Milestone: --- With -fstrict-flex-arrays, there is a clearer definition of what constitutes flexible array members, controlled by the frontend. tree-object-size however doesn't seem to handle this well enough when the flex array is nested. e.g.: typedef struct { char pad; char data[8]; } F2; typedef struct { unsigned pad; F2 flex; } S2; #define NULL (void *) 0 __SIZE_TYPE__ nested_flexarray (__SIZE_TYPE__ n) { S2 *p = (S2 *) __builtin_malloc (n); return __builtin_dynamic_object_size (p->flex.data, 1); } __SIZE_TYPE__ nested_flexarray2 (S2 *p) { return __builtin_dynamic_object_size (p->flex.data, 1); } The current behaviour is to treat data as a flex array and nested_flexarray returns the size as if it were a flex array. nested_flexarray2 however returns the subscripted size, thinking of it as a fixed array, which should not be the expected behaviour with -fstrict-flex-arrays=0. Instead it should return -1 for maximum size and perhaps 8 as minimum size. If F2 is changed to: typedef struct { char pad; char data[0]; } F2; the current behaviour ends up returning 0 in both cases, which is incorrect. Again, it should return the size as if it were a flex array in nested_flexarray and -1 for maximum size for nested_flexarray2. I have a patch that does this, but I need to fix up tests that currently expect older behaviour and wanted to get some consensus before I fixed them up.