From patchwork Fri Aug 11 14:50:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 109884 Delivered-To: patch@linaro.org Received: by 10.182.109.195 with SMTP id hu3csp974978obb; Fri, 11 Aug 2017 07:51:35 -0700 (PDT) X-Received: by 10.98.245.20 with SMTP id n20mr16311004pfh.46.1502463095667; Fri, 11 Aug 2017 07:51:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502463095; cv=none; d=google.com; s=arc-20160816; b=ruLmiU9SaixuxrS/6zXklRsaHgXcHrVkIT7sq2NSG2NjphCJTgOPxJ24EabXFpCMEl 8/RkH1KUbM5XlR7YZHSNVES+pYsez+JBkQyATn6C4UqKLDN3tRa4Yi3zzxUv+OBBSWix U90wfIUcdtJlyxViys7zg1It8sroOPR4i7wDN9nmJ2Bk9T5YcO1ETtH2NxE+aYdemuMO VU/JiStFlDcIJfifNzl4vCnJhUR3eMJJZmuFBSAQl1+6cgNSYXSNvEajcLreCCh/Pcc2 ba3iWAcPEel/y9M0Sr3NhfpbCDc8rAv6eZfD2+17YtjY7IY2wqPNW5+EPuJgKegSwl8e tkbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :delivered-to:sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=sn1P6YM2tOXmyhOWPxdDfHRtfuq6bU/sqzVEIDZG0lo=; b=ii4KqtsZ5AhSbrYoSe/7lAUgtfDZ0ICsypi1h3Opb+QS+kHCXQTkuwFLFZeBCjj14s R3v4UrOAWRaDxaJl+8SJhHHYrFEV0QwsnAea4Hy28ATmQxWTIWWjBIv3nkAzjnfNKZws HszZxQt2XBqAAooIGbZSP4Ns/kUVmWOc5QPW7ClROX3WYEjFy7y92TX8/uPGY4xWmEGh UIRhV0B9Z5VN7Kz4sWWU2fhE4bDW9x5MmQTYddi3O/3FEf9VoAYnCFosbnSEHHSb0ouf 20rcRibXda/ONosxm8VPgIQwH2ZCTGnGyQIQdJUxI4AYiQn05JEryj76UxYLY48VKq8B xImg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=idsgSlAD; spf=pass (google.com: domain of libc-alpha-return-83026-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83026-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id s23si657523plk.743.2017.08.11.07.51.35 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Aug 2017 07:51:35 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-83026-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=idsgSlAD; spf=pass (google.com: domain of libc-alpha-return-83026-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83026-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=pq1PzIfaMt9v+wR+KASTvQdpKwzWA+z iVGnToq71Woio+/OF6ijSGnKr73aux+43UAoEd328dKaH66e0hz753zIr+zdukC9 24wzWuh4aP33voU9oNriMiL6JXBqdi2JJMKmm2FEhJRmdTKdprZeafjCvDnypC0Z pqtld9heZ+SQ= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=tMqgoOyrctlpqsbPqMeTIceiRbg=; b=idsgS lADLnRGW9GazCZf/is4BfnffYoVPNMWD8IyLAqAwq8DqvnjPD6stzGWwmJM3P8XN oDPad7V9E762A5Be2a/5yv3tMWXstbPCJKosTfnWoLFBBKy1kL4jRQx26NJsy/j2 7fbGcQVoXwbrUNYYq10oQcMijBkp0trF50Y584= Received: (qmail 71043 invoked by alias); 11 Aug 2017 14:51:01 -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 70747 invoked by uid 89); 11 Aug 2017 14:51:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS, UNSUBSCRIBE_BODY autolearn=ham version=3.3.2 spammy=9249 X-HELO: mail-qt0-f173.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=sn1P6YM2tOXmyhOWPxdDfHRtfuq6bU/sqzVEIDZG0lo=; b=bYyAKQkPdpWDlJ2n2G21fGBoJwR0hJOFmF7SD18JgesH3g7A2bd10Un6R4NaBInKob kX0q6g+pQSkwFwzOH3Cf6y3b7ESmUYkS66EJByRgIVYgDwtRWCNefTtdSZjuTdcAftDo W8bqJWYxMiLd/hgt0/9x3qHRKuZV/ceNJCDcnt3rRUuoE6Y1U4kwV/i1xKB4ONgt+zlU swkL/DVg0hBcMUdqp91z+ZELzazEpMZfugOIIfULbSWbbw15bAQ1dlAiORKepmZEEgRQ jJjDcJVSbUwAWOR+e14CAi8gr54WI2OCXfLTUvw1UOFUevFWt6/10Bv1SyVywEwtWxnA wzXw== X-Gm-Message-State: AHYfb5ghLYxnSWP6ertzI8O+coyadBqtYEEliCeU16DzuVYe934vwgFe 6df/NbflhSGWcl6TQ19Fug== X-Received: by 10.237.36.38 with SMTP id r35mr23011097qtc.327.1502463056610; Fri, 11 Aug 2017 07:50:56 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Florian Weimer Subject: [PATCH 05/18] posix: Rewrite to use struct scratch_buffer instead of extend_alloca Date: Fri, 11 Aug 2017 11:50:31 -0300 Message-Id: <1502463044-4042-6-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1502463044-4042-1-git-send-email-adhemerval.zanella@linaro.org> References: <1502463044-4042-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(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 7b6b426..99e9c54 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -89,12 +89,8 @@ #include #include +#include -#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,13 +924,9 @@ 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 @@ -1031,6 +935,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), goto out; } } + scratch_buffer_free (&pwtmpbuf); } } # endif /* Not Amiga && not WINDOWS32. */