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 7/8] posix: Use char_array for home_dir in glob
Date: Tue,  5 Jan 2021 15:58:19 -0300	[thread overview]
Message-ID: <20210105185820.3796657-8-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20210105185820.3796657-1-adhemerval.zanella@linaro.org>

This patch uses dynarray at glob internal home directory ame
manipulation for GLOB_TILDE.  It simplifies it and removes all the
boilerplate buffer managements required.

Checked x86_64-linux-gnu.
---
 posix/glob.c | 69 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 27 deletions(-)

diff --git a/posix/glob.c b/posix/glob.c
index c126b4501d..b6727ee884 100644
--- a/posix/glob.c
+++ b/posix/glob.c
@@ -606,10 +606,15 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
              && (char_array_pos (&dirname, 2) == '\0'
                 || char_array_pos (&dirname, 2) == '/')))
         {
+          struct char_array home_dir;
+
+          const char *home_env = getenv ("HOME");
+          home_env = home_env == NULL ? "" : home_env;
+          if (!char_array_init_str (&home_dir, home_env))
+            goto err_nospace;
+
+          if (char_array_is_empty (&home_dir))
           /* Look up home directory.  */
-          char *home_dir = getenv ("HOME");
-          int malloc_home_dir = 0;
-          if (home_dir == NULL || home_dir[0] == '\0')
             {
 #ifdef WINDOWS32
               /* Windows NT defines HOMEDRIVE and HOMEPATH.  But give
@@ -619,16 +624,21 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
 
               if (home_drive != NULL && home_path != NULL)
                 {
-                  size_t home_drive_len = strlen (home_drive);
-                  size_t home_path_len = strlen (home_path);
-                  char *mem = alloca (home_drive_len + home_path_len + 1);
-
-                  memcpy (mem, home_drive, home_drive_len);
-                  memcpy (mem + home_drive_len, home_path, home_path_len + 1);
-                  home_dir = mem;
+                  if (!char_array_set_str (&home_dir, home_drive)
+                      || !char_array_append_str (&home_dir, home_path))
+                   {
+                     char_array_free (&home_dir);
+                     goto err_nospace;
+                   }
                 }
               else
-                home_dir = "c:/users/default"; /* poor default */
+                {
+                  if (!char_array_set_str (&home_dir, "c:/users/default"))
+                    {
+                      char_array_free (&home_dir);
+                      goto err_nospace;
+                    }
+                }
 #else
               int err;
               struct passwd *p;
@@ -657,44 +667,49 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
                   if (!scratch_buffer_grow (&s))
                   goto err_nospace;
                 }
-              if (err == 0)
-                {
-                  home_dir = strdup (p->pw_dir);
-                  malloc_home_dir = 1;
-                }
+              if (err == 0 && !char_array_set_str (&home_dir, p->pw_dir))
+                err = 1;
               scratch_buffer_free (&s);
-              if (err == 0 && home_dir == NULL)
-              goto err_nospace;
+              if (err == 0)
+                goto err_nospace;
 #endif /* WINDOWS32 */
             }
-          if (home_dir == NULL || home_dir[0] == '\0')
+          if (char_array_is_empty (&home_dir))
             {
-              if (__glibc_unlikely (malloc_home_dir))
-                free (home_dir);
               if (flags & GLOB_TILDE_CHECK)
                 {
+                  char_array_free (&home_dir);
                   retval = GLOB_NOMATCH;
                   goto out;
                 }
               else
                 {
-                  home_dir = (char *) "~"; /* No luck.  */
-                  malloc_home_dir = 0;
+                  if (!char_array_set_str (&home_dir, "~"))
+                    {
+                      char_array_free (&home_dir);
+                      goto err_nospace;
+                    }
                 }
             }
           /* Now construct the full directory.  */
+          bool e = true;
           if (char_array_pos (&dirname, 1) == '\0')
             {
-              if (!char_array_set_str (&dirname, home_dir))
-                goto err_nospace;
+              e = char_array_set_str (&dirname, char_array_str (&home_dir));
               dirlen = char_array_size (&dirname) - 1;
             }
           else
             {
               /* Replaces '~' by the obtained HOME dir.  */
               char_array_erase (&dirname, 0);
-              if (!char_array_prepend_str (&dirname, home_dir))
-               goto err_nospace;
+              e = char_array_prepend_str (&dirname,
+                                          char_array_str (&home_dir));
+            }
+          if (e == false)
+            {
+              char_array_free (&dirname);
+              char_array_free (&home_dir);
+              goto err_nospace;
             }
           dirname_modified = true;
         }
-- 
2.25.1


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

Thread overview: 14+ 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 ` [PATCH 6/8] posix: Remove alloca usage on glob user_name Adhemerval Zanella
2021-01-05 18:58 ` Adhemerval Zanella [this message]
2021-01-05 18:58 ` [PATCH 8/8] posix: Remove all alloca usage in glob 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 7/8] posix: Use char_array for home_dir in glob Adhemerval Zanella
2017-11-21 14:24   ` Andreas Schwab

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-8-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).