[PATCH 4/6] posix: Use malloc instead of alloca for the glob brace expansion

Adhemerval Zanella <[email protected]> Mon, 3 Aug 2026 16:19:18 -0300
Newsgroups gmane.comp.lib.glibc.alpha
Message-ID <[email protected]>
The last alloca in __glob is the buffer holding one expansion of a
brace expression.  As with the directory and user names, the stack it
takes is not bounded by the call itself.

Use malloc unconditionally.  __glob no longer uses alloca; glob_in_dir
still does, so the accounting stays for now.

Checked on x86_64-linux-gnu, aarch64-linux-gnu, and i686-linux-gnu.
---
 posix/glob.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)

diff --git a/posix/glob.c b/posix/glob.c
index a9d69dde24a..8d78145fe7e 100644
--- a/posix/glob.c
+++ b/posix/glob.c
@@ -483,15 +483,10 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
           size_t rest_len;
           char *onealt;
           size_t pattern_len = strlen (pattern) - 1;
-          int alloca_onealt = glob_use_alloca (alloca_used, pattern_len);
-          if (alloca_onealt)
-            onealt = alloca_account (pattern_len, alloca_used);
-          else
-            {
-              onealt = malloc (pattern_len);
-              if (onealt == NULL)
-                return GLOB_NOSPACE;
-            }
+
+          onealt = malloc (pattern_len);
+          if (onealt == NULL)
+            return GLOB_NOSPACE;
 
           /* We know the prefix for all sub-patterns.  */
           alt_start = mempcpy (onealt, pattern, begin - pattern);
@@ -503,8 +498,7 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
             {
               /* It is an invalid expression.  */
             illegal_brace:
-              if (__glibc_unlikely (!alloca_onealt))
-                free (onealt);
+              free (onealt);
               flags &= ~GLOB_BRACE;
               goto no_brace;
             }
@@ -545,8 +539,7 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
               /* If we got an error, return it.  */
               if (result && result != GLOB_NOMATCH)
                 {
-                  if (__glibc_unlikely (!alloca_onealt))
-                    free (onealt);
+                  free (onealt);
                   if (!(flags & GLOB_APPEND))
                     {
                       globfree (pglob);
@@ -564,8 +557,7 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
               assert (next != NULL);
             }
 
-          if (__glibc_unlikely (!alloca_onealt))
-            free (onealt);
+          free (onealt);
 
           if (pglob->gl_pathc != firstc)
             /* We found some entries.  */
-- 
2.53.0