Re: [PATCH] git-compat-util(msvc): C11 does not imply support for zero-sized arrays
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Dec 9, 2021, 21:18 UTC
- Message-ID
- <xmqqfsr1pjdo.fsf@gitster.g>
- In-Reply-To
- <f245f2b7-ca00-5497-3d1d-658c4c0fbcb6@gmail.com>
Phillip Wood <phillip.wood123@gmail.com> writes:
Show 8 quoted lines
>> the end of a struct isn't triggering a compile error. But having a field
>> _after_ that empty flex array _does_ trigger a compile error:
>> struct MyStruct {
>> int x;
>> int flexA[];
>> char string[256];
>> };
>> Note the added `string` field.Ick.
If we use the "safer but a bit wasteful" alternative in such a case, it will become just wrong. We'll have a single-member array there, and extra allocation for an instance of MyStruct will only lengthen the .string member, without allowing the .flexA member to have more elements.
Show 22 quoted lines
> Having a field after the flex array is a violation of the C
> standard. Section 6.7.2.1:
> ... the last member of a structure with more than one named member
> may have incomplete array type, such a structure (and any union
> containing, possibly recursively, a member that is such a structure)
> shall not be a member of a structure or an element of an array.
>
> Note that the wording also forbids
>
> struct A {
> int x;
> char flex[];
> };
>
> struct B {
> struct A a; /* This is forbidden */
> };
>
> There was a proposal a few years ago to relax that restriction [1] but
> it does not seem to be in the latest draft standard.
>
> None of this helps fix the problem, but it does explain why MSVC complains.Thanks.
In short, the code is wrong, and the compiler is complaining to avoid us using a wrong code. If the same code is given to other compilers that support ANCI C style flexible array member, we are likely to see the same error.
Which makes the whole thing (including my "let's make the conditionals to set up FLEX_ARRAY for various compilers easier to read") much less urgent.
Thanks for a well-written summary.