From: Bruno Haible <bruno@clisp.org>
Date: Mon, 14 Apr 2025 14:00:13 +0000 (+0200)
Subject: regex: Fix undefined behaviour.
X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=2798f2cb8466ff02056a4afbd0629d21e78afa80;p=gnulib.git

regex: Fix undefined behaviour.

* lib/regex.h (_REGEX_NELTS): Define to empty; don't use ISO C99
variable-length arrays.
---

diff --git a/ChangeLog b/ChangeLog
index 62c91fca9e..5e49fce290 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-04-14  Bruno Haible  <bruno@clisp.org>
+
+	regex: Fix undefined behaviour.
+	* lib/regex.h (_REGEX_NELTS): Define to empty; don't use ISO C99
+	variable-length arrays.
+
 2025-04-14  Bruno Haible  <bruno@clisp.org>
 
 	select tests: Work around a Cygwin bug.
diff --git a/lib/regex.h b/lib/regex.h
index 67a3aa70a5..164dbe1115 100644
--- a/lib/regex.h
+++ b/lib/regex.h
@@ -523,8 +523,12 @@ typedef struct
 /* Declarations for routines.  */
 
 #ifndef _REGEX_NELTS
-# if (defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__ \
-	&& !defined __STDC_NO_VLA__)
+/* The macro _REGEX_NELTS denotes the number of elements in a variable-length
+   array passed to a function.
+   It was meant to make use of ISO C99 variable-length arrays, but this does
+   not work: ISO C23 § 6.7.6.2.(5) requires the number of elements to be > 0,
+   but the NMATCH argument to regexec() is allowed to be 0.  */
+# if 0
 #  define _REGEX_NELTS(n) n
 # else
 #  define _REGEX_NELTS(n)