From patchwork Thu Jun 8 21:13:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 20856 Received: (qmail 38543 invoked by alias); 8 Jun 2017 21:13:58 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 38443 invoked by uid 89); 8 Jun 2017 21:13:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS, UNSUBSCRIBE_BODY autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f179.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=S9NkWFtoWCPwibyNfQLlxTWcXfbQzaOWTw6TyEwtquU=; b=ND1maLQ5TIRMbZn47ez1umUbR5EDxpkcbAMXEchzNdZdoJy3LnILlNvSx/S3A0xOrm AdA+5ThGGbDpKH09gXU4MjBbohUkyJ8dZsd6qTTNz6yTyaZI1ELl/4BkNuO1g6yj5GFE kRMQMtbTHLT83W90ypvL7As7xwSjx6osCqzudnnzeVKoF1SoEoa2RCBQYLVV935goQvv RHKPQNCiIY1y1lWBBD8wiYEPLF3gDsvK4Vrt1gbjKFpL54FI6DECtUlVFsWDDocbj6+u iAfGonxrK2xHaxMA9ScsxZ/YBwpfMODdn2jd79ARNP+XJ85gqlH37BOm9pk2R6JFB1pj EpQw== X-Gm-Message-State: AKS2vOztJregezTZpwfvnzsbONVdXMdUAv7qIkwycj2CW9b5zyFR5MNX i2ez9RFiJjw3ecSQALDBGw== X-Received: by 10.55.120.199 with SMTP id t190mr44400741qkc.235.1496956437601; Thu, 08 Jun 2017 14:13:57 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Florian Weimer Subject: [PATCH 06/17] posix: Rewrite to use struct scratch_buffer instead of extend_alloca Date: Thu, 8 Jun 2017 18:13:20 -0300 Message-Id: <1496956411-25594-7-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> From: Florian Weimer This patch removes a lot of boilerplate code to manager buffers for getpwnam_r. Checked on x86_64-linux-gnu. [BZ #18023] * posix/glob.c (glob): Use struct scratch_buffer instead of extend_alloca. --- posix/glob.c | 147 +++++++++++------------------------------------------------ 1 file changed, 26 insertions(+), 121 deletions(-) diff --git a/posix/glob.c b/posix/glob.c index dc13e26..425d81b 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -88,13 +88,9 @@ #include +#include #include "glob_internal.h" -#ifdef _SC_GETPW_R_SIZE_MAX -# define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX) -#else -# define GETPW_R_SIZE_MAX() (-1) -#endif #ifdef _SC_LOGIN_NAME_MAX # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX) #else @@ -696,97 +692,43 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (success) { struct passwd *p; - char *malloc_pwtmpbuf = NULL; - char *pwtmpbuf; # if defined HAVE_GETPWNAM_R || defined _LIBC - long int pwbuflenmax = GETPW_R_SIZE_MAX (); - size_t pwbuflen = pwbuflenmax; struct passwd pwbuf; int save = errno; + struct scratch_buffer pwtmpbuf; + scratch_buffer_init (&pwtmpbuf); -# ifndef _LIBC - if (! (0 < pwbuflenmax && pwbuflenmax <= SIZE_MAX)) - /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. - Try a moderate value. */ - pwbuflen = 1024; -# endif - if (glob_use_alloca (alloca_used, pwbuflen)) - pwtmpbuf = alloca_account (pwbuflen, alloca_used); - else - { - pwtmpbuf = malloc (pwbuflen); - if (pwtmpbuf == NULL) - { - if (__glibc_unlikely (malloc_name)) - free (name); - retval = GLOB_NOSPACE; - goto out; - } - malloc_pwtmpbuf = pwtmpbuf; - } - - while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p) + while (getpwnam_r (name, &pwbuf, + pwtmpbuf.data, pwtmpbuf.length, &p) != 0) { - size_t newlen; - bool v; if (errno != ERANGE) { p = NULL; break; } - v = size_add_wrapv (pwbuflen, pwbuflen, &newlen); - if (!v && malloc_pwtmpbuf == NULL - && glob_use_alloca (alloca_used, newlen)) - pwtmpbuf = extend_alloca_account (pwtmpbuf, pwbuflen, - newlen, alloca_used); - else + if (!scratch_buffer_grow (&pwtmpbuf)) { - char *newp = (v ? NULL - : realloc (malloc_pwtmpbuf, newlen)); - if (newp == NULL) - { - free (malloc_pwtmpbuf); - if (__glibc_unlikely (malloc_name)) - free (name); - retval = GLOB_NOSPACE; - goto out; - } - malloc_pwtmpbuf = pwtmpbuf = newp; + retval = GLOB_NOSPACE; + goto out; } - pwbuflen = newlen; __set_errno (save); } # else - p = getpwnam (name); + p = getpwnam (pwtmpbuf.data); # endif - if (__glibc_unlikely (malloc_name)) - free (name); if (p != NULL) { - if (malloc_pwtmpbuf == NULL) - home_dir = p->pw_dir; - else + home_dir = strdup (p->pw_dir); + malloc_home_dir = 1; + if (home_dir == NULL) { - size_t home_dir_len = strlen (p->pw_dir) + 1; - if (glob_use_alloca (alloca_used, home_dir_len)) - home_dir = alloca_account (home_dir_len, - alloca_used); - else - { - home_dir = malloc (home_dir_len); - if (home_dir == NULL) - { - free (pwtmpbuf); - retval = GLOB_NOSPACE; - goto out; - } - malloc_home_dir = 1; - } - memcpy (home_dir, p->pw_dir, home_dir_len); + scratch_buffer_free (&pwtmpbuf); + retval = GLOB_NOSPACE; + goto out; } } - free (malloc_pwtmpbuf); + scratch_buffer_free (&pwtmpbuf); } else { @@ -924,59 +866,25 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* Look up specific user's home directory. */ { struct passwd *p; - char *malloc_pwtmpbuf = NULL; + struct scratch_buffer pwtmpbuf; + scratch_buffer_init (&pwtmpbuf); + # if defined HAVE_GETPWNAM_R || defined _LIBC - long int buflenmax = GETPW_R_SIZE_MAX (); - size_t buflen = buflenmax; - char *pwtmpbuf; struct passwd pwbuf; int save = errno; -# ifndef _LIBC - if (! (0 <= buflenmax && buflenmax <= SIZE_MAX)) - /* Perhaps 'sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a - moderate value. */ - buflen = 1024; -# endif - if (glob_use_alloca (alloca_used, buflen)) - pwtmpbuf = alloca_account (buflen, alloca_used); - else - { - pwtmpbuf = malloc (buflen); - if (pwtmpbuf == NULL) - { - nomem_getpw: - if (__glibc_unlikely (malloc_user_name)) - free (user_name); - retval = GLOB_NOSPACE; - goto out; - } - malloc_pwtmpbuf = pwtmpbuf; - } - - while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0) + while (getpwnam_r (user_name, &pwbuf, + pwtmpbuf.data, pwtmpbuf.length, &p) != 0) { - size_t newlen; - bool v; if (errno != ERANGE) { p = NULL; break; } - v = size_add_wrapv (buflen, buflen, &newlen); - if (!v && malloc_pwtmpbuf == NULL - && glob_use_alloca (alloca_used, newlen)) - pwtmpbuf = extend_alloca_account (pwtmpbuf, buflen, - newlen, alloca_used); - else + if (!scratch_buffer_grow (&pwtmpbuf)) { - char *newp = v ? NULL : realloc (malloc_pwtmpbuf, newlen); - if (newp == NULL) - { - free (malloc_pwtmpbuf); - goto nomem_getpw; - } - malloc_pwtmpbuf = pwtmpbuf = newp; + retval = GLOB_NOSPACE; + goto out; } __set_errno (save); } @@ -1005,7 +913,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), dirname = malloc (home_len + rest_len + 1); if (dirname == NULL) { - free (malloc_pwtmpbuf); + scratch_buffer_free (&pwtmpbuf); retval = GLOB_NOSPACE; goto out; } @@ -1016,18 +924,15 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), dirlen = home_len + rest_len; dirname_modified = 1; - - free (malloc_pwtmpbuf); } else { - free (malloc_pwtmpbuf); - if (flags & GLOB_TILDE_CHECK) /* We have to regard it as an error if we cannot find the home directory. */ return GLOB_NOMATCH; } + scratch_buffer_free (&pwtmpbuf); } } # endif /* Not Amiga && not WINDOWS32. */