[3/6] posix: Use malloc instead of alloca for the glob user name

Message ID 20260803192144.1727862-4-adhemerval.zanella@linaro.org (mailing list archive)
State Committed
Commit d0084cbd1835b344eb2cb14f00e486a676fc0623
Headers
Series posix: Do not recurse once per pattern component in glob (BZ #34453) |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

Adhemerval Zanella Netto Aug. 3, 2026, 7:19 p.m. UTC
  Use malloc unconditionally.  The name is only needed for the passwd
lookup that follows, which is far more expensive than the allocation.

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

Comments

Collin Funk Aug. 6, 2026, 1:42 a.m. UTC | #1
Adhemerval Zanella <adhemerval.zanella@linaro.org> writes:

> Use malloc unconditionally.  The name is only needed for the passwd
> lookup that follows, which is far more expensive than the allocation.
>
> Checked on x86_64-linux-gnu, aarch64-linux-gnu, and i686-linux-gnu.
> ---
>  posix/glob.c | 16 +++++-----------
>  1 file changed, 5 insertions(+), 11 deletions(-)

Looks good.

It annoys me slightly that we use "int" instead of "bool" there, but
that is separate from your patches. :)

Reviewed-by: Collin Funk <collin.funk1@gmail.com>

Collin
  

Patch

diff --git a/posix/glob.c b/posix/glob.c
index 1b1fd8eda02..a9d69dde24a 100644
--- a/posix/glob.c
+++ b/posix/glob.c
@@ -826,19 +826,13 @@  __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
             user_name = dirname + 1;
           else
             {
-              char *newp;
-              if (glob_use_alloca (alloca_used, end_name - dirname))
-                newp = alloca_account (end_name - dirname, alloca_used);
-              else
+              char *newp = malloc (end_name - dirname);
+              if (newp == NULL)
                 {
-                  newp = malloc (end_name - dirname);
-                  if (newp == NULL)
-                    {
-                      retval = GLOB_NOSPACE;
-                      goto out;
-                    }
-                  malloc_user_name = 1;
+                  retval = GLOB_NOSPACE;
+                  goto out;
                 }
+              malloc_user_name = 1;
               if (unescape != NULL)
                 {
                   char *p = mempcpy (newp, dirname + 1,