From patchwork Mon Feb 5 13:27:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 126881 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp1971797ljc; Mon, 5 Feb 2018 05:28:54 -0800 (PST) X-Google-Smtp-Source: AH8x225rL/6Q1FafKBlYQGRdCmCZRb6PmJF6njdYWuR5YwCrP1+oa9Ig+4TIy6L9mgv3nHVrMasK X-Received: by 2002:a17:902:6a16:: with SMTP id m22-v6mr45338028plk.142.1517837334643; Mon, 05 Feb 2018 05:28:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517837334; cv=none; d=google.com; s=arc-20160816; b=s3OjMynoyRlazkIvSKEltqei+T77aDRocSOhPPP26QATkCDsXt8TJl+pzW3KueEImW OxJAoH61WfisfVHkErNREUXFiDrqA8k/CoAxtTbkt/6Vn1o/EcIfdMMRbNckTzM/iCz3 gudYws4pMgwcd/qHxdqr2O3iPXgdcVYghUhcK6pRIrkz7Nvaphd2M0RPau1wXMti96Ua 8t/33J28Ilyk9fVMnXBnvbkRjKfX/0UmuhcYUQBKr9lYgzJraH3gVyqq/Xr3TliSjCtE beomziEfeLxWMSqUbg0XBeotjVraD91hV/8EK2TSgmyOCfoXDsNI/WYnNnWT8lMIl/h5 9RrQ== 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=PXCCHChDoVPOP8u0wH8xaEmB/sEpbx2BgABOmp9YnDc=; b=QtIKZJ4WQflYRYFg+MKMusObHiaBlukGPvdTYlFxN6+cHIPGos1R5hReELShyA6zio 82t4U+ZVQ0qq/aSJ87Lm2CBYv8Toc6vKfLMwn7YJPSEZAz2tDeNiZufQh4hibDlmFpgQ zVoFtqc0jkrFfme9AAWhqhHcLy2zFCHbF68vTDxQ5i9eM5S+VzQ0NFAbSWma03q0c7YY o+qMN6PEErXtZUziEB+w9DitvN/PssW2h6NTcBSnN+fnbGeSiZA2G6HvJ2Fhi7sDYJOu 42CfdmMmd8LUIVc37zxXFf2VRuhO+ihXKJWrkBV0yxoDHmaxIFGU2LoSk4kPaFglWqTb 1DCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=DdFBypDV; spf=pass (google.com: domain of libc-alpha-return-89970-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89970-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 y34-v6si6899741plb.403.2018.02.05.05.28.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Feb 2018 05:28:54 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-89970-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=DdFBypDV; spf=pass (google.com: domain of libc-alpha-return-89970-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89970-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=WNBfTsnjxAXSvJoDqXAAxuuQfVuKx0U azz9WBX7GsFSGnzYu+xFiN4ghzu1Wf/o+laYlToxIFa4QSEH/x/1FcW/UjY2JYjq jPIYys1zn09Sj8dfJp0laF+Fw4eTHSzNV2GE89N65+p2bI/q9jAr0fB0/HfTRKsg XYaU+6LTlZck= 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=S4GZrd7PYrygxIkJad3slOchMDk=; b=DdFBy pDVPZP6IvW43EcNP9YUKaTOkx54H72ycFZFTkR5nCM75XWgkvCloGAkN6idkhHu1 w+huTfkTgJeF25IxDMvF60EEDT2iCf1zaOkYEqPVTl/YEmzGbKQCAL/lQpz+m4P2 KDDOqC9oArRTrtPGmz0dD2CWNllry+4yRvomb0= Received: (qmail 33172 invoked by alias); 5 Feb 2018 13:27:55 -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 32998 invoked by uid 89); 5 Feb 2018 13:27:54 -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, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f195.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=PXCCHChDoVPOP8u0wH8xaEmB/sEpbx2BgABOmp9YnDc=; b=RtpKZpqRCAubL+wR9drjPv8ve8f70eqKNPJv0NGmZJqP0G/a5iys+XvyB1d04WCWsE /3wHCwp6Lu7URW4wIdgLceZ/FLtOU9tDSL3OK0d88dvEveLMbVp1k0n31zfRa1iUFAPo MurT16tmbUKh/q00s9AVMwjTanaohCtJOUGuN52oFhz6LAMYybIUHUHnCgCTl4+GBySg oSNNKjyoUcInk0bKPkTj5LbuplKyl1SVrvM/9sYYkJwjr6XfHd9L+qPb1zZTWUS7i6ev zMN92OEczFVHamEl+98pCwxCKNF8kZssv8dXe6CRXWFWN7tmcJ+Yy+1cSqkzKVeBJ8WC 02Eg== X-Gm-Message-State: AKwxytfA+ZysjOOn0TAW86FHPH3Rukae39YrCvXevXkjwfv4xEYXD0Ku T5jAqhAhM5/ZnbqtXlRBAxUI3+zuwzQ= X-Received: by 10.237.55.233 with SMTP id j96mr80327182qtb.192.1517837269962; Mon, 05 Feb 2018 05:27:49 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 06/12] posix: Remove alloca usage on glob user_name Date: Mon, 5 Feb 2018 11:27:28 -0200 Message-Id: <1517837254-19399-7-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1517837254-19399-1-git-send-email-adhemerval.zanella@linaro.org> References: <1517837254-19399-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 2632e93..e914748 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); @@ -713,11 +690,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)) { @@ -731,27 +707,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 == '\\') @@ -762,20 +730,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. */ @@ -787,7 +756,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) { @@ -798,11 +767,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) @@ -1025,9 +993,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 {