public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org, Paul Eggert <eggert@cs.ucla.edu>,
	bug-gnulib@gnu.org
Subject: [PATCH 6/8] posix: Remove alloca usage on glob user_name
Date: Tue,  5 Jan 2021 15:58:18 -0300	[thread overview]
Message-ID: <20210105185820.3796657-7-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20210105185820.3796657-1-adhemerval.zanella@linaro.org>

This patch uses dynarray at glob internal user name manipulation for
GLOB_TILDE.  It simplifies it and removes all the boilerplate buffer
managements required.  It also removes the glob_use_alloca, since
it is not used anymore.

Checked on x86_64-linux-gnu.
---
 posix/glob.c | 86 ++++++++++++++++------------------------------------
 1 file changed, 26 insertions(+), 60 deletions(-)

diff --git a/posix/glob.c b/posix/glob.c
index d199204931..c126b4501d 100644
--- a/posix/glob.c
+++ b/posix/glob.c
@@ -42,13 +42,13 @@
 
 #ifndef WINDOWS32
 # include <pwd.h>
+# include <alloca.h>
 #endif
 
 #include <errno.h>
 #include <dirent.h>
 #include <stdlib.h>
 #include <string.h>
-#include <alloca.h>
 
 #ifdef _LIBC
 # undef strdup
@@ -215,29 +215,6 @@ glob_lstat (glob_t *pglob, int flags, const char *fullname)
           : LSTAT64 (fullname, &ust.st64));
 }
 
-/* Set *R = A + B.  Return true if the answer is mathematically
-   incorrect due to overflow; in this case, *R is the low order
-   bits of the correct answer.  */
-
-static bool
-size_add_wrapv (size_t a, size_t b, size_t *r)
-{
-#if 7 <= __GNUC__ && !defined __ICC
-  return __builtin_add_overflow (a, b, r);
-#else
-  *r = a + b;
-  return *r < a;
-#endif
-}
-
-static bool
-glob_use_alloca (size_t alloca_used, size_t len)
-{
-  size_t size;
-  return (!size_add_wrapv (alloca_used, len, &size)
-          && __libc_use_alloca (size));
-}
-
 static int glob_in_dir (const char *pattern, const char *directory,
                         int flags, int (*errfunc) (const char *, int),
                         glob_t *pglob, size_t alloca_used);
@@ -724,11 +701,10 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
       else
         {
 #ifndef WINDOWS32
-          char *dirnamestr = char_array_at (&dirname, 0);
-          char *end_name = strchr (dirnamestr, '/');
-          char *user_name;
-          int malloc_user_name = 0;
-          char *unescape = NULL;
+          const char *dirnamestr = char_array_str (&dirname);
+          const char *end_name = strchr (dirnamestr, '/');
+          struct char_array user_name;
+          const char *unescape = NULL;
 
           if (!(flags & GLOB_NOESCAPE))
             {
@@ -742,27 +718,19 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
               unescape = memchr (dirnamestr, '\\', end_name - dirnamestr);
             }
           if (end_name == NULL)
-            user_name = dirnamestr + 1;
+            {
+              if (!char_array_init_str (&user_name, dirnamestr + 1))
+                goto err_nospace;
+            }
           else
             {
-              char *newp;
-              if (glob_use_alloca (alloca_used, end_name - dirnamestr))
-                newp = alloca_account (end_name - dirnamestr, alloca_used);
-              else
-                {
-                  newp = malloc (end_name - dirnamestr);
-                  if (newp == NULL)
-                    {
-                      retval = GLOB_NOSPACE;
-                      goto out;
-                    }
-                  malloc_user_name = 1;
-                }
               if (unescape != NULL)
                 {
-                  char *p = mempcpy (newp, dirnamestr + 1,
-                                     unescape - dirnamestr - 1);
-                  char *q = unescape;
+                  ptrdiff_t name_len = unescape - dirnamestr - 1;
+                  if (!char_array_init_str_size (&user_name, dirnamestr + 1,
+                                                 name_len))
+                    goto err_nospace;
+                  const char *q = unescape;
                   while (q != end_name)
                     {
                       if (*q == '\\')
@@ -773,20 +741,21 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
                                  but "~fo\\o\\/" unescape to user_name
                                  "foo".  */
                               if (filename == NULL)
-                                *p++ = '\\';
+                                char_array_append_char (&user_name, '\\');
                               break;
                             }
                           ++q;
                         }
-                      *p++ = *q++;
+                      char_array_append_char (&user_name, *q++);
                     }
-                  *p = '\0';
                 }
               else
-                *((char *) mempcpy (newp, dirnamestr + 1,
-                                    end_name - dirnamestr - 1))
-                   = '\0';
-              user_name = newp;
+                {
+                  ptrdiff_t name_len = end_name - dirnamestr - 1;
+                  if (!char_array_init_str_size (&user_name, dirnamestr + 1,
+                                                 name_len))
+                    goto err_nospace;
+                }
             }
 
           /* Look up specific user's home directory.  */
@@ -798,7 +767,7 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
 #  if defined HAVE_GETPWNAM_R || defined _LIBC
             struct passwd pwbuf;
 
-            while (getpwnam_r (user_name, &pwbuf,
+            while (getpwnam_r (char_array_str (&user_name), &pwbuf,
                                pwtmpbuf.data, pwtmpbuf.length, &p)
                    == ERANGE)
               {
@@ -809,11 +778,10 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
                   }
               }
 #  else
-            p = getpwnam (user_name);
+            p = getpwnam (char_array_str (&user_name));
 #  endif
 
-            if (__glibc_unlikely (malloc_user_name))
-              free (user_name);
+            char_array_free (&user_name);
 
             /* If we found a home directory use this.  */
             if (p != NULL)
@@ -1038,9 +1006,7 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
       if (meta & GLOBPAT_BACKSLASH)
         {
           char *p = strchr (char_array_str (&dirname), '\\'), *q;
-          /* We need to unescape the dirname string.  It is certainly
-             allocated by alloca, as otherwise filename would be NULL
-             or dirname wouldn't contain backslashes.  */
+          /* We need to unescape the dirname string.  */
           q = p;
           do
             {
-- 
2.25.1


  parent reply	other threads:[~2021-01-05 18:58 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-05 18:58 [PATCH 0/8] Remove alloca usage from glob Adhemerval Zanella
2021-01-05 18:58 ` [PATCH 1/8] malloc: Add specialized dynarray for C strings Adhemerval Zanella
2021-01-05 18:58 ` [PATCH 2/8] posix: Use char_array for internal glob dirname Adhemerval Zanella
2021-03-23 16:08   ` Arjun Shankar
2021-03-24 17:39     ` Adhemerval Zanella
2021-01-05 18:58 ` [PATCH 3/8] posix: Remove alloca usage for GLOB_BRACE on glob Adhemerval Zanella
2021-01-05 18:58 ` [PATCH 4/8] posix: Remove alloca usage on glob dirname Adhemerval Zanella
2021-01-05 18:58 ` [PATCH 5/8] posix: Use dynarray for globname in glob Adhemerval Zanella
2021-01-05 18:58 ` Adhemerval Zanella [this message]
2021-01-05 18:58 ` [PATCH 7/8] posix: Use char_array for home_dir " Adhemerval Zanella
2021-01-05 18:58 ` [PATCH 8/8] posix: Remove all alloca usage " Adhemerval Zanella
2021-01-13 19:36 ` [PATCH 0/8] Remove alloca usage from glob Paul Eggert
  -- strict thread matches above, loose matches on Subject: below --
2017-11-21 13:55 [PATCH 0/8] posix: glob fixes and refactor Adhemerval Zanella
2017-11-21 13:55 ` [PATCH 6/8] posix: Remove alloca usage on glob user_name Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210105185820.3796657-7-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).