From patchwork Tue Nov 21 13:55:28 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: 119358 Delivered-To: patch@linaro.org Received: by 10.80.225.132 with SMTP id k4csp5125146edl; Tue, 21 Nov 2017 05:56:54 -0800 (PST) X-Google-Smtp-Source: AGs4zMaIoChcFE+00fhv/PXDrXffxI2/lhzevjniFt3M4AURd0lQl69hhgLd3JuZVFBSk8XKkFEk X-Received: by 10.99.112.69 with SMTP id a5mr17070349pgn.179.1511272614588; Tue, 21 Nov 2017 05:56:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511272614; cv=none; d=google.com; s=arc-20160816; b=DNonRQDBrYRvWI4A+uK80X8AlcBIJOM/ftEauAV22+dhizTrtu0aiNEdgjrMR21h1R s6UXOoh9DRA8dqNk3hE8on6ERu5yA9hvSq0v7W8jmdmUTzV0q6CDxXEUsUKKfeBwKrrG SHZh6YrDA80IVbqprEsdADNZ63gP5u5WLmb6us7QIPa2qceHkxH+GgLhidb1tYHWI3kb tIfJ+PmabvejVepjcj/xXRTphSCJwQnLltZaADG/3ZuGM9vv1s0FL9QUpGPVkEWDcAlI c+4XeeSVPC8qjt6IiXGqDxXWePxxtiDlJKHvor2NGCFI8SZYlrEgrJCQ/A3oQpPo5hY+ 5KjA== 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: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=BxkRTBC9ywApgvrUMQ/OY1Tj0z9ioEHiT85vRvy/MUw=; b=m2lo/CF1/Apk44U0sEbANSSwqYTi+wIgB6Y1uywq7bP2G9i1XLNw6WNCoMd44sfhGW ja03j4orwWyT1CeV5ULaCqurziLVzmudIc9FM2k5+fBnTc+b1ndPUHUS+Go/9s6WG7jE 2Sz7nnu6A260MeO3q+WJJ5iAqr5dw6cdAokUFS5bfRjDzL5MxYaxW2dHqT0hhb7a11Zt +35nFMsjC03Ok5QvERi9zQtwolorXSGKJCepgtTgwt5+yLWvhSMCMxi1ux5V2C58WqyK nzVFnLhwPH0jFwTDGOLuWhTmY0UQh8TXuA/Dcy7oAajtbQD7Pbbo5TrN9OaA19ZsEwFH tm+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=UTxHS8l1; spf=pass (google.com: domain of libc-alpha-return-87362-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-87362-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 s14si11004430pgr.330.2017.11.21.05.56.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Nov 2017 05:56:54 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-87362-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=UTxHS8l1; spf=pass (google.com: domain of libc-alpha-return-87362-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-87362-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:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=jCDyL5nzptDcaL//snXh3rj1p16GwPn T4R4DK/dcwoaiuWzp4EYkjujHbUVaxhPDPxuH86sKaVaMEO6DG1PoxqxTeupi31i eUlULKR3liqWlutajUlxayviwIFx0IsHRYa3I/+J6ETPGnYiSshZBijLo60tVZdf nojUa6U/7ICA= 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:subject:date:message-id:in-reply-to :references; s=default; bh=UDFwuhV5JdstxmHXoL4YNHNAexA=; b=UTxHS 8l1fPy+3RVxueaUmIVeSxeAJlpx0PTRV4AaH6iqVnpOlB+9b3fK14xdpIOPTFR66 ibCMHZh/MWdDYAsHpP2GCF2Uuk21nc9GMr0azD+UeavAbVPTRqu5nUi4ytb/jzrg 3dVke+Wpsl+prId8CrwJ0aHPhbvhKY4vUGnJFg= Received: (qmail 44841 invoked by alias); 21 Nov 2017 13:55:50 -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 44816 invoked by uid 89); 21 Nov 2017 13:55:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KB_WAM_FROM_NAME_SINGLEWORD, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f194.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=BxkRTBC9ywApgvrUMQ/OY1Tj0z9ioEHiT85vRvy/MUw=; b=VABnFeX45pHZTYUUYM5bpJAVF9KoWXTGkiMd+gSH6x/xSL7W0JHRErwGQZ1jBx8xn9 SPq3fY3vc6TGyYitLGpM19rzvhYVcU26/nnJec1IHMNgueJx+TSW8jX2hsJPxzvKhsOY F71ixdbYA/s04AbgqJtgpxka7uf4EiTxx0NRXtT8NokdVWCI+2vPkeP0AJB1cLWM6Xes lmDvWTN4XTUO9jUAcUrM6kyrWDSn0RE5HQmWFsHd3MQsjRb8VTSMMDKtoxzLpBdjZvoI fRLrHb5x/yoVXpSNYHAxiTOhKoZSEgI749e8din/nJvq6C25r3iqXun4hYGK1XNrqVX2 6jWg== X-Gm-Message-State: AJaThX7EkjLKEVHk/gaAtXjeUTrx0CH0kU/RMQa0gUrT7wljD7rYCpa/ 2fGPaT3bqCvgGMuIfytnlSHhnSbCVuM= X-Received: by 10.200.55.53 with SMTP id o50mr26750177qtb.68.1511272545951; Tue, 21 Nov 2017 05:55:45 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 6/8] posix: Remove alloca usage on glob user_name Date: Tue, 21 Nov 2017 11:55:28 -0200 Message-Id: <1511272530-10936-7-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1511272530-10936-1-git-send-email-adhemerval.zanella@linaro.org> References: <1511272530-10936-1-git-send-email-adhemerval.zanella@linaro.org> This patch uses dynarray at glob internal user name manipulation for GLOB_TILDE. It simplifies it and removes all the boilerplate buffer managements required. It also removes the glob_use_alloca, since it is not used anymore. Checked on x86_64-linux-gnu. * posix/glob.c (size_add_wrapv, glob_use_alloca): Remove. (__glob): Use char_array for user name. Signed-off-by: Adhemerval Zanella --- ChangeLog | 3 +++ posix/glob.c | 86 ++++++++++++++++++------------------------------------------ 2 files changed, 29 insertions(+), 60 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 59baf62..aefdb28 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -32,13 +32,13 @@ #ifndef WINDOWS32 # include +# include #endif #include #include #include #include -#include #ifdef _LIBC # undef strdup @@ -205,29 +205,6 @@ glob_lstat (glob_t *pglob, int flags, const char *fullname) : LSTAT64 (fullname, &ust.st64)); } -/* Set *R = A + B. Return true if the answer is mathematically - incorrect due to overflow; in this case, *R is the low order - bits of the correct answer. */ - -static bool -size_add_wrapv (size_t a, size_t b, size_t *r) -{ -#if 5 <= __GNUC__ && !defined __ICC - return __builtin_add_overflow (a, b, r); -#else - *r = a + b; - return *r < a; -#endif -} - -static bool -glob_use_alloca (size_t alloca_used, size_t len) -{ - size_t size; - return (!size_add_wrapv (alloca_used, len, &size) - && __libc_use_alloca (size)); -} - static int glob_in_dir (const char *pattern, const char *directory, int flags, int (*errfunc) (const char *, int), glob_t *pglob, size_t alloca_used); @@ -712,11 +689,10 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int), else { #ifndef WINDOWS32 - char *dirnamestr = char_array_at (&dirname, 0); - char *end_name = strchr (dirnamestr, '/'); - char *user_name; - int malloc_user_name = 0; - char *unescape = NULL; + const char *dirnamestr = char_array_str (&dirname); + const char *end_name = strchr (dirnamestr, '/'); + struct char_array user_name; + const char *unescape = NULL; if (!(flags & GLOB_NOESCAPE)) { @@ -730,27 +706,19 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int), unescape = memchr (dirnamestr, '\\', end_name - dirnamestr); } if (end_name == NULL) - user_name = dirnamestr + 1; + { + if (!char_array_init_str (&user_name, dirnamestr + 1)) + goto err_nospace; + } else { - char *newp; - if (glob_use_alloca (alloca_used, end_name - dirnamestr)) - newp = alloca_account (end_name - dirnamestr, alloca_used); - else - { - newp = malloc (end_name - dirnamestr); - if (newp == NULL) - { - retval = GLOB_NOSPACE; - goto out; - } - malloc_user_name = 1; - } if (unescape != NULL) { - char *p = mempcpy (newp, dirnamestr + 1, - unescape - dirnamestr - 1); - char *q = unescape; + ptrdiff_t name_len = unescape - dirnamestr - 1; + if (!char_array_init_str_size (&user_name, dirnamestr + 1, + name_len)) + goto err_nospace; + const char *q = unescape; while (q != end_name) { if (*q == '\\') @@ -761,20 +729,21 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int), but "~fo\\o\\/" unescape to user_name "foo". */ if (filename == NULL) - *p++ = '\\'; + char_array_append_char (&user_name, '\\'); break; } ++q; } - *p++ = *q++; + char_array_append_char (&user_name, *q++); } - *p = '\0'; } else - *((char *) mempcpy (newp, dirnamestr + 1, - end_name - dirnamestr - 1)) - = '\0'; - user_name = newp; + { + ptrdiff_t name_len = end_name - dirnamestr - 1; + if (!char_array_init_str_size (&user_name, dirnamestr + 1, + name_len)) + goto err_nospace; + } } /* Look up specific user's home directory. */ @@ -786,7 +755,7 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int), # if defined HAVE_GETPWNAM_R || defined _LIBC struct passwd pwbuf; - while (getpwnam_r (user_name, &pwbuf, + while (getpwnam_r (char_array_str (&user_name), &pwbuf, pwtmpbuf.data, pwtmpbuf.length, &p) == ERANGE) { @@ -797,11 +766,10 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } } # else - p = getpwnam (user_name); + p = getpwnam (char_array_str (&user_name)); # endif - if (__glibc_unlikely (malloc_user_name)) - free (user_name); + char_array_free (&user_name); /* If we found a home directory use this. */ if (p != NULL) @@ -1024,9 +992,7 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (meta & GLOBPAT_BACKSLASH) { char *p = strchr (char_array_str (&dirname), '\\'), *q; - /* We need to unescape the dirname string. It is certainly - allocated by alloca, as otherwise filename would be NULL - or dirname wouldn't contain backslashes. */ + /* We need to unescape the dirname string. */ q = p; do {