git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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.
Previous: Phillip WoodNext: Junio C Hamano
Message 8 of 10 in “git-compat-util(msvc): C11 does not imply support for zero-sized arrays”
  1. git-compat-util(msvc): C11 does not imply support for zero-sized arraysJohannes Schindelin via GitGitGadget, Dec 6, 2021
  2. Junio C HamanoDec 6, 2021
  3. Neeraj SinghDec 6, 2021
  4. Johannes SchindelinDec 7, 2021
  5. Neeraj SinghDec 7, 2021
  6. rsbecker@nexbridge.comDec 7, 2021
  7. Phillip WoodDec 9, 2021
  8. Junio C HamanoDec 9, 2021
  9. Junio C HamanoDec 9, 2021
  10. Neeraj SinghDec 9, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.