threads / patch / 64611

patch3.0: require C99 flexible-array member syntax

Subject: [PATCH] 3.0: require C99 flexible-array member syntax

## tl;dr

3 messages between Dec 11, 2025 and Dec 12, 2025. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Junio C Hamano· Dec 11, 2025, 10:16 UTC · lore

Before C99 syntax to express that the final member in a struct is an array of unknown number of elements, i.e.,

	struct {
		...
		T flexible_array[];
	};

came along, GNU introduced their own extension to declare such a member with 0 size, i.e.,

		T flexible_array[0];

and the compilers that did not understand even that were given a way to emulate it by wasting one element, i.e.,

		T flexible_array[1];

As we are pushing more and more C99 language features, let's declare the historical forms of flexible array member support obsolete and require C99 syntax from all compilers that want to compile Git.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/BreakingChanges.adoc |  3 +++
 git-compat-util.h                  | 31 +++++++++++++++++--------------
 2 files changed, 20 insertions(+), 14 deletions(-)
Show changes to diff +20 −14
diff --git c/Documentation/BreakingChanges.adoc w/Documentation/BreakingChanges.adoc
index f814450d2f..81ab3cd1d8 100644
--- c/Documentation/BreakingChanges.adoc
+++ w/Documentation/BreakingChanges.adoc
@@ -315,6 +315,9 @@ symbolic links are not supported on some platforms.
 Note that only the writing side for such symbolic links is deprecated. Reading
 such symbolic links is still supported for now.
 
+* Support for flexible array member emulation using FLEX_ARRAY macro
+  for compilers that do not understand C99 FAM syntax will be removed.
+
 == Superseded features that will not be deprecated
 
 Some features have gained newer replacements that aim to improve the design in
diff --git c/git-compat-util.h w/git-compat-util.h
index 398e0fac4f..cedd022396 100644
--- c/git-compat-util.h
+++ w/git-compat-util.h
@@ -38,7 +38,10 @@ struct strbuf;
 DISABLE_WARNING(-Wsign-compare)
 #endif
 
-#ifndef FLEX_ARRAY
+#ifdef WITH_BREAKING_CHANGES
+# define FLEX_ARRAY /* C99 FAM mandatory */
+#else
+# ifndef FLEX_ARRAY
 /*
  * See if our compiler is known to support flexible array members.
  */
@@ -50,25 +53,25 @@ DISABLE_WARNING(-Wsign-compare)
  * here, we can fall back to use the "safer but a bit wasteful" one
  * later.
  */
-#if defined(__SUNPRO_C) && (__SUNPRO_C <= 0x580)
-#elif defined(__GNUC__)
-# if (__GNUC__ >= 3)
+# if defined(__SUNPRO_C) && (__SUNPRO_C <= 0x580)
+# elif defined(__GNUC__)
+#  if (__GNUC__ >= 3)
+#   define FLEX_ARRAY /* empty */
+#  else
+#   define FLEX_ARRAY 0 /* older GNU extension */
+#  endif
+# elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
 #  define FLEX_ARRAY /* empty */
-# else
-#  define FLEX_ARRAY 0 /* older GNU extension */
 # endif
-#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
-# define FLEX_ARRAY /* empty */
-#endif
 
 /*
  * Otherwise, default to safer but a bit wasteful traditional style
  */
-#ifndef FLEX_ARRAY
-# define FLEX_ARRAY 1
-#endif
-#endif
-
+# ifndef FLEX_ARRAY
+#  define FLEX_ARRAY 1
+# endif
+# endif /* FLEX_ARRAY */
+#endif /* WITH_BREAKING_CHANGES */
 
 /*
  * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression.
Patrick Steinhardt· Dec 12, 2025, 08:36 UTC · re: Junio C Hamano · lore

Re: [PATCH] 3.0: require C99 flexible-array member syntax

On Thu, Dec 11, 2025 at 07:16:13PM +0900, Junio C Hamano wrote:
Show 21 quoted lines
> Before C99 syntax to express that the final member in a struct is an
> array of unknown number of elements, i.e.,
> 
> 	struct {
> 		...
> 		T flexible_array[];
> 	};
> 
> came along, GNU introduced their own extension to declare such a
> member with 0 size, i.e.,
> 
> 		T flexible_array[0];
> 
> and the compilers that did not understand even that were given a way
> to emulate it by wasting one element, i.e.,
> 
> 		T flexible_array[1];
> 
> As we are pushing more and more C99 language features, let's declare
> the historical forms of flexible array member support obsolete and
> require C99 syntax from all compilers that want to compile Git.

Is there any specific reason why this is tied to the 3.0 breaking changes document? I would have expected that we introduce this change via a test balloon like we usually do for new C features that we haven't used before. And we already are using C99 features, so I wouldn't consider this change to be "more breaking" than any of the other C99 features we have introduced already.

So I wonder whether we should instead convert one of the sites that currently uses FLEX_ARRAY to use C99 flexible arrays unconditionally. On the other hand we don't really gain much by doing that, as it is just as easy to adapt the FLEX_ARRAY definitions to unconditionally use C99 flexible arrays. Both would boil down to a couple lines of changes, only.

If we see that any platform out there doesn't support this feature we can still roll back and maybe tie it to Git 3.0.

Thanks!
Patrick
Junio C Hamano· Dec 12, 2025, 12:54 UTC · re: Patrick Steinhardt · lore

Re: [PATCH] 3.0: require C99 flexible-array member syntax

Patrick Steinhardt <ps@pks.im> writes:
Show 6 quoted lines
>> As we are pushing more and more C99 language features, let's declare
>> the historical forms of flexible array member support obsolete and
>> require C99 syntax from all compilers that want to compile Git.
>
> Is there any specific reason why this is tied to the 3.0 breaking
> changes document?
Convenience, mostly.

It is not like we weren't allowing use of an advanced syntax and then trying to see if everybody can use it, which was perfect match for our past "weather balloon" approach. Rather, we know that many platforms did not know how to grok fam[] with an empty brackets, and have workarounds fam[0] and fam[1]. This change proposes to break the platforms that has to rely on the latter.

> So I wonder whether we should instead convert one of the sites that
> currently uses FLEX_ARRAY to use C99 flexible arrays unconditionally.

OK, that is easy enough. Or just remove it altogether and see who screams, like the attached.

----- >8 -----
Subject: [PATCH] FLEX_ARRAY: require platforms to support the C99 syntax

Before C99 syntax to express that the final member in a struct is an array of unknown number of elements, i.e.,

	struct {
		...
		T flexible_array[];
	};

came along, GNU introduced their own extension to declare such a member with 0 size, i.e.,

		T flexible_array[0];

and the compilers that did not understand even that were given a way to emulate it by wasting one element, i.e.,

		T flexible_array[1];

As we are using more and more C99 language features, let's see if the platforms that still need to resort to the historical forms of flexible array member support are still there, by forcing all the flex array definitions to use the C99 syntax and see if anybody screams (in which case reverting the changes is rather easy).

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * In the longer term, I'd love to import the fortified memcpy()
   invented for the Linux kernel project that takes advantage of
   advanced compiler features like __builtin_object_dynamic_size()
   and friends, and use of C99 flexible array syntax is a good first
   step to make it happen.
 git-compat-util.h | 33 ++-------------------------------
 1 file changed, 2 insertions(+), 31 deletions(-)
Show changes to diff +2 −31
diff --git c/git-compat-util.h w/git-compat-util.h
index 398e0fac4f..8e3f3a58a3 100644
--- c/git-compat-util.h
+++ w/git-compat-util.h
@@ -38,37 +38,8 @@ struct strbuf;
 DISABLE_WARNING(-Wsign-compare)
 #endif
 
-#ifndef FLEX_ARRAY
-/*
- * See if our compiler is known to support flexible array members.
- */
-
-/*
- * Check vendor specific quirks first, before checking the
- * __STDC_VERSION__, as vendor compilers can lie and we need to be
- * able to work them around.  Note that by not defining FLEX_ARRAY
- * here, we can fall back to use the "safer but a bit wasteful" one
- * later.
- */
-#if defined(__SUNPRO_C) && (__SUNPRO_C <= 0x580)
-#elif defined(__GNUC__)
-# if (__GNUC__ >= 3)
-#  define FLEX_ARRAY /* empty */
-# else
-#  define FLEX_ARRAY 0 /* older GNU extension */
-# endif
-#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
-# define FLEX_ARRAY /* empty */
-#endif
-
-/*
- * Otherwise, default to safer but a bit wasteful traditional style
- */
-#ifndef FLEX_ARRAY
-# define FLEX_ARRAY 1
-#endif
-#endif
-
+#undef FLEX_ARRAY
+#define FLEX_ARRAY /* empty - weather balloon to require C99 FAM */
 
 /*
  * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression.

← back to recent threads