From patchwork Thu Jun 8 21:13:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 20860 Received: (qmail 39323 invoked by alias); 8 Jun 2017 21:14:05 -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 39146 invoked by uid 89); 8 Jun 2017 21:14:04 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-27.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 autolearn=ham version=3.3.2 spammy=accomplish X-HELO: mail-qt0-f181.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:subject:date:message-id:in-reply-to :references; bh=WAaG+BIGQi8QXi6C1pzSbHUrOHLs5nok0Qy6oeJhEMQ=; b=cTTxCnpe094IoM6VNo3GEk8P3EGvWyDJc1En/Ciy4JnHbIc0NNTRK6UaCTnVZBuw8Q U/rcg2Qmo6z0F7PMxQwlR3zYLLnLnPeuUY9+q3QzNjTwELGqr0fjjW+DRpFXkbjAhKji p/YZxqNUkyRsCccArVfQDefJ7iLwhk629JHR5VJ1PIFM9c4IX2BNGkog7mII3l8O6oeb vmkTyr8jZr1qGfGnkBiPk09n3kimMB4dl6G+DYRgHE7GVIWFKYqjODk2pO2SBDKpkA7h 43GqANdmXYT4rjfTbX8aXijPdwMzg41ib3hzBJgHlXRHVg+ykLvSwCO83Y9IWNWeZ/31 7IGQ== X-Gm-Message-State: AKS2vOx9oOBgyzZxY5Z3wTWlCG/hj/TcBG8Niuw9ibScBuzVCXjZblvv 61HbGQuyeu14fiItuDUiaw== X-Received: by 10.55.95.5 with SMTP id t5mr46885253qkb.64.1496956445155; Thu, 08 Jun 2017 14:14:05 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 09/17] posix: Remove glob GET_LOGIN_NAME_MAX usage Date: Thu, 8 Jun 2017 18:13:23 -0300 Message-Id: <1496956411-25594-10-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> Current glob implementation allows non limited user name for home directory construction on GLOB_TILDE case. To accomplish it glob either construct a name on stack if size are small enough (based on current alloca_used) value in heap otherwise. There is no actual login to resize the buffer in case of the resizing the buffer in case of ERANGE, so a static buffer using glibc default LOGIN_NAME_MAX is suffice. Checked on x86_64-linux-gnu. * posix/glob.c (LOGIN_NAME_MAX): Define if not defined. (glob): Use static buffer for user_name on getlogin_r. --- posix/glob.c | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/posix/glob.c b/posix/glob.c index fbb7aa7..14a502c 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -92,10 +92,8 @@ #include "glob_internal.h" #include -#ifdef _SC_LOGIN_NAME_MAX -# define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX) -#else -# define GET_LOGIN_NAME_MAX() (-1) +#ifndef LOGIN_NAME_MAX +# define LOGIN_NAME_MAX 256 #endif static const char *next_brace_sub (const char *begin, int flags) __THROWNL; @@ -677,25 +675,9 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (home_dir == NULL || home_dir[0] == '\0') { int success; - char *name; - int malloc_name = 0; - size_t buflen = GET_LOGIN_NAME_MAX () + 1; - - if (buflen == 0) - /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try - a moderate value. */ - buflen = 20; - if (glob_use_alloca (alloca_used, buflen)) - name = alloca_account (buflen, alloca_used); - else - { - name = malloc (buflen); - if (name == NULL) - goto err_nospace; - malloc_name = 1; - } + char user_name[LOGIN_NAME_MAX]; - success = __getlogin_r (name, buflen) == 0; + success = __getlogin_r (user_name, sizeof (user_name)) == 0; if (success) { struct passwd *p; @@ -705,7 +687,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), struct scratch_buffer pwtmpbuf; scratch_buffer_init (&pwtmpbuf); - while (getpwnam_r (name, &pwbuf, + while (getpwnam_r (user_name, &pwbuf, pwtmpbuf.data, pwtmpbuf.length, &p) != 0) { @@ -733,11 +715,6 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } scratch_buffer_free (&pwtmpbuf); } - else - { - if (__glibc_unlikely (malloc_name)) - free (name); - } } if (home_dir == NULL || home_dir[0] == '\0') {