Bug 114266 - No -pedantic diagnostic for zero-sized array in compound literals
Summary: No -pedantic diagnostic for zero-sized array in compound literals
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 14.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2024-03-07 10:50 UTC by Daniel Lundin
Modified: 2024-03-08 07:25 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-03-07 00:00:00


Attachments
Complete example (71 bytes, text/plain)
2024-03-07 10:52 UTC, Daniel Lundin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Lundin 2024-03-07 10:50:44 UTC
(int[]){}; produces no diagnostics with -std=c23 -pedantic.

Empty initalizer lists are OK under -std=c23 and -std=gnu so that's not the problem. gcc correctly gives a diagnostic for that in -std=c17 -pedantic mode.

Zero-sized arrays should however give a diagnostic in -std=cxx -pedantic mode. Prior to C23 this was not really a problem as we would get a diagnostic for the empty initializer list. But now this non-conforming C compiles cleanly even with -pedantic set.

A diagnostic similar to the one given in clang would be good:
warning: zero size arrays are an extension [-Wzero-length-array]

As I understand it, gcc did have -Wzero-length-array in the past but removed it? See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94428. The options -Wstrict-flex-arrays -fstrict-flex-arrays seem to have no effect (and my example isn't a flexible array member but a compound literal).
Comment 1 Daniel Lundin 2024-03-07 10:52:04 UTC
Created attachment 57643 [details]
Complete example
Comment 2 Joseph S. Myers 2024-03-07 16:43:02 UTC
The relevant constraint here is "An array of unknown size shall not be initialized by an empty initializer.".
Comment 3 Daniel Lundin 2024-03-08 07:25:33 UTC
(In reply to Joseph S. Myers from comment #2)
> The relevant constraint here is "An array of unknown size shall not be
> initialized by an empty initializer.".

Indeed! I didn't realize it was also a constraint (C23 6.7.10). That probably means that a diagnostic warning should always be given and not just in -pedantic mode.