From patchwork Tue Sep 5 20:25:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 111742 Delivered-To: patch@linaro.org Received: by 10.140.94.166 with SMTP id g35csp3323599qge; Tue, 5 Sep 2017 13:26:53 -0700 (PDT) X-Received: by 10.99.126.87 with SMTP id o23mr5071176pgn.277.1504643213712; Tue, 05 Sep 2017 13:26:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1504643213; cv=none; d=google.com; s=arc-20160816; b=wMk7Nrj+AJPbj1A2HtR3RDAMxd8k9cW8GnOUAiyGKCUgEbJF+RMEfcMwrM0Jr0h9Fo r/I96Uxq7lVVGtFdPjO7KxfFTsB8kRF21hYsiqleu+5Woi/lsbOeuyrmvx434lk8/QHq vPRGy67LPpoz5TIBnPQAckaH+zPmZOvK28pobzXB5EpLZpQwqGgqq7q89jVi4pNqKS6n kL1L/omWYTflNWOM5tu8UdIYLUBg6UnwgX/FB+6J3ENDjMAUj0s/vT5UFo7Xxxh0Z/xs 4MsD2Nwd64me3W9ILpEGQ4FXUOVBN+u9y4R5u5pPqsCghr/WsTo9c/uGhS3vll64ndYW 0K1A== 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=Blvulj+f48B14LwO6vJPYfxom6ZpkU8t9dDAAMQv7GU=; b=rZnnFyGlhy888gy1fwkkiCWpJorxz6JTf9CfpCDSs7kfqT+fLB35a9n517FSjgWhuy CafRdDl6cCWeXUnCEnbzNfxYExcyYYz6Odx8XbjkWNkwScWYlJnZpBw5NbQTD21CvccE 95fx+/Dmru04WHiynpDKqmwRr3smGe51WFgkb6YVHr19+xC1h+6hNff0zFmUzgOHiVL5 5ckihm62lJJVNarm3lcQUubw37Wxz5EMLyUCxSfW3ScWPbUdinrgXVsxnj1fRTQNA6O4 mYr5s6v757d8PQhsnvuvZrpc3U5/yO38VrIPctMoquw5aYUTEFBRszcUjMZR/PshUHoL wwvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=LsGESjwz; spf=pass (google.com: domain of libc-alpha-return-84212-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-84212-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 1si930787plh.340.2017.09.05.13.26.53 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Sep 2017 13:26:53 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-84212-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=LsGESjwz; spf=pass (google.com: domain of libc-alpha-return-84212-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-84212-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=nt/UsOUrVijeEwxbtG/mKLuty1A9LOh JAWC1nQFwk1pNB9X7hGtb2GYRTg4RA6aHjjSBn5IpOl4TCYKoHh7x+BF5jzixdUr l7F/e1aiXQM2C94P0mWL+1oy11h1NXLeJwa7F6bOZRwN731Ft3Ct8HM90mt4MXJe yU06Kgoh9mx8= 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=ugzsZmLO86wRXxXbmWjH9WRq9EA=; b=LsGES jwzaZrUcbzgyewDclVr8ubl9sUfIWiifIMZ3yAZh0OClBbPVSJp5/KKrbZkNc+EE irJ/x1LbfzB1n+Jgfe4IeJ7CNa/FqAKtwFK8kOj7szEyETevE9xUzy7N/McqIbf8 /+qH+joeWpdC84vEkYj/kUCrbJhbNarIANLGKY= Received: (qmail 49483 invoked by alias); 5 Sep 2017 20:25:54 -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 48835 invoked by uid 89); 5 Sep 2017 20:25:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=lacking X-HELO: mail-qk0-f169.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=Blvulj+f48B14LwO6vJPYfxom6ZpkU8t9dDAAMQv7GU=; b=fDSqlKXFgrzZ2SQAyBX8sV7T2S5slA83sq9SP4//wLL227xLUasxNH+V0I4885Eclj y+BrgqOhwfgh+gyoqg2/GCKGv7QYgum5lQB+m5QzASknLJBRiiW5C0APTpCJkHcuoau8 gB55X4cK6Nt/mP81eLLog+C5xGvTINhn6lSNTWvbeWr1VkMnRmjK0f+rGzsZUCYQNwPw QRAoe6ScIUcLHdQ3h2E5KuXpMY6CQzktbIcHh3BYPrDN7FavYpPmjvci6ho7GIzEFQgf mehFKUsDsZhCzJu++NBDnDFnKQdmmbMi6DU+qFQLEpRXsamzQ8ES1Fg0jQVmb6KeZX3i rFbg== X-Gm-Message-State: AHPjjUgd7uoCSwYlAu18uYCFUTBZBCOsivHEscQFYFhmuaBU2mgIzzfo cCE//VcL5NTiX400JfBNGA== X-Google-Smtp-Source: ADKCNb7UK1hbNrfLf1Vphvq8IlWY/RDOXlSCq6dfo4PdNDrqBUrYgD9amna9jF77wlte53QJwNuNHQ== X-Received: by 10.55.86.134 with SMTP id k128mr501528qkb.56.1504643140870; Tue, 05 Sep 2017 13:25:40 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Paul Eggert Subject: [PATCH 5/9] posix: Fix getpwnam_r usage (BZ #1062) Date: Tue, 5 Sep 2017 17:25:18 -0300 Message-Id: <1504643122-14874-6-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1504643122-14874-1-git-send-email-adhemerval.zanella@linaro.org> References: <1504643122-14874-1-git-send-email-adhemerval.zanella@linaro.org> This patch fixes longstanding misuse of errno after getpwnam_r, which returns an error number rather than setting errno. This is sync with gnulib commit 5db9301. Checked on x86_64-linux-gnu and on a build using build-many-glibcs.py for all major architectures. [BZ #1062] * posix/glob.c (glob): Port recent patches to platforms lacking getpwnam_r. (glob): Fix longstanding misuse of errno after getpwnam_r, which returns an error number rather than setting errno. --- ChangeLog | 6 +++ posix/glob.c | 164 +++++++++-------------------------------------------------- 2 files changed, 30 insertions(+), 140 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 15c6295..340cf08 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -15,10 +15,6 @@ License along with the GNU C Library; if not, see . */ -#ifndef _LIBC -# include -#endif - #include #include @@ -39,10 +35,6 @@ #endif #include -#ifndef __set_errno -# define __set_errno(val) errno = (val) -#endif - #include #include #include @@ -82,12 +74,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 @@ -649,97 +637,36 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (success) { struct passwd *p; - char *malloc_pwtmpbuf = NULL; - char *pwtmpbuf; + struct scratch_buffer pwtmpbuf; + scratch_buffer_init (&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; -# 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 + while (getpwnam_r (name, &pwbuf, + pwtmpbuf.data, pwtmpbuf.length, &p) + == ERANGE) { - pwtmpbuf = malloc (pwbuflen); - if (pwtmpbuf == NULL) + if (!scratch_buffer_grow (&pwtmpbuf)) { - if (__glibc_unlikely (malloc_name)) - free (name); retval = GLOB_NOSPACE; goto out; } - malloc_pwtmpbuf = pwtmpbuf; - } - - while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &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 - { - 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; - } - pwbuflen = newlen; - __set_errno (save); } # else p = getpwnam (name); # 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 { @@ -876,61 +803,21 @@ 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 + + while (getpwnam_r (user_name, &pwbuf, + pwtmpbuf.data, pwtmpbuf.length, &p) + == ERANGE) { - pwtmpbuf = malloc (buflen); - if (pwtmpbuf == NULL) + if (!scratch_buffer_grow (&pwtmpbuf)) { - 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) - { - 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 - { - char *newp = v ? NULL : realloc (malloc_pwtmpbuf, newlen); - if (newp == NULL) - { - free (malloc_pwtmpbuf); - goto nomem_getpw; - } - malloc_pwtmpbuf = pwtmpbuf = newp; - } - __set_errno (save); } # else p = getpwnam (user_name); @@ -957,7 +844,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; } @@ -968,13 +855,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 @@ -983,6 +866,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), goto out; } } + scratch_buffer_free (&pwtmpbuf); } #endif /* !WINDOWS32 */ }