From patchwork Fri Aug 11 14:50:36 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: 109890 Delivered-To: patch@linaro.org Received: by 10.182.109.195 with SMTP id hu3csp976341obb; Fri, 11 Aug 2017 07:52:49 -0700 (PDT) X-Received: by 10.99.168.65 with SMTP id i1mr15349208pgp.305.1502463169136; Fri, 11 Aug 2017 07:52:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502463169; cv=none; d=google.com; s=arc-20160816; b=ruyCH3M8h/HYcWQWvXdGa95lZLjSm7Sgf7kDBP9PRAxv4eRD7J1WyfTXQKi7RlragG 5TTAXQkGGa7dsMgjt75F8T/Mm5cD1idXfR9biCY8CK1ACk8K60v9cuh5AFUNxN96BbZE /nWGN47Hx3OWdctnOTwowMIAylbDNlRTmW1UuCus0KkYZspZy8nc0F9+nzzD2NRookv5 ReRYelmauedeXKZghJZ8NRvyKgsXs80RzN+PvteuSr+sADq3bJiJaoWvuvTvEUtLTV+V nMXqqKfpUPPDW6Nq0Wf9GXVTGssD+5l5yEK0e+WO0hWgVHONkxs9ZSVyGEIU0vxtyBUa SvUw== 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=CVItzNj6c3jt5td88j9oqqu+qwCZQh97EKJ76pCttvY=; b=Itbbsu2No6u3jnpql3dvRBiLSC15LfEEhBCTInlYF+5VORoiGFuMJdhGZD221g00Mj PN/uihGJAsFewUlmKQiczEpkGvvXqIEwcZP9VBAzhBMbSSGjrX5aSM9GJ4bikNEAePRK GHT7NZuDllYrJ1EtPSfT4jZteqeExu0PrPWwKe+jU2qGo6RUU0bO5iJpLE0oIhDBGkHV eOP1pTqG0JEbwZyskLd440GqHz4xsnDENlymyz1tQTHRqhMFyKWmr+e7UCvynn50ct6t 06D/ZgOkOzVeIJhDu+pTOjRTjNN30JA67x/g/Yz/jpHKDqq9KGxIsySgbkDbRA71hwwG gIKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=AXvr3LIN; spf=pass (google.com: domain of libc-alpha-return-83032-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83032-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 v123si622351pfv.124.2017.08.11.07.52.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Aug 2017 07:52:49 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-83032-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=AXvr3LIN; spf=pass (google.com: domain of libc-alpha-return-83032-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83032-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=lvjGIMWT2yW0k1iI1UGDEMfE37B2Kpe IRhBGvA3NW1uJZEHyJgGq40p2bJ/CjFvDV62Ccn0+XXrE/S871qup/24phYGIDuX MjP4jIUvy3mtljynS5k3TKV3NlbJg3Rib1Gr7CxoqhwCZJyLRGEBoFX+SnjpmX7x qSoyo3l3ff3M= 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=T8FZxtXGtdxHTSVRw2j0B20S9+8=; b=AXvr3 LIN7PXuA4TrnBTIPHFC7JxUuN0g+DtVyzUcT5CdMgR30aKuQHLD1MnLouu5ZDJeZ OAB1ZRIZQMIN/VYCaUekJWDonVNNkSUOPeYlFD10n+dqCeJz98EC36jUrc1TDlsA LwiAOKqqgt/joZObkob+43kml6IEj1MlMkVz6w= Received: (qmail 78104 invoked by alias); 11 Aug 2017 14:51:14 -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 74843 invoked by uid 89); 11 Aug 2017 14:51:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.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 autolearn=ham version=3.3.2 spammy=4448 X-HELO: mail-qk0-f179.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=CVItzNj6c3jt5td88j9oqqu+qwCZQh97EKJ76pCttvY=; b=YeH7iYXSKxLE7CSBaWMnKcPCvmZfZ53MOQqXdiHIHQ5robNtzUU8zel2RvKhRnKIPO oxNo4fIzDDmWS+ePmGoyydf2SZuiT6A6j2YXxnWWiTHP1X3Zp/19c61TvOBdEuN9C4Xr W9Rr1cTKCYihzmu5YDPvLg8kKGj/Ls8p6eTHLIOoTERXgdiuXxbEXtFXUSNPYtsI9TLR hgMDKPOg92kwcC+T1JRnUQTOvGqNjhiA129uPCAppG/QmPQ1ngPG3FUPkLOv6/Rq56Dw wNirxG8HA37wiPtuUKrAV7CDY/1+Q6wHF9SfEaLPCWvZ4rAJhWbx4ayCAGRdu3HJ65yN nF6A== X-Gm-Message-State: AHYfb5gU5WyQXo89f5LCCVTYES/XMf9ZwmTVqDrTB1ASH8CbSZYEw6+y mLOYMcEw2xQ3zMmAg/2u3g== X-Received: by 10.55.5.13 with SMTP id 13mr11102631qkf.120.1502463064017; Fri, 11 Aug 2017 07:51:04 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 10/18] posix: Remove alloca usage for GLOB_BRACE on glob Date: Fri, 11 Aug 2017 11:50:36 -0300 Message-Id: <1502463044-4042-11-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> GNU GLOB_BRACE internal implementation constructs a new expression and calls glob recursively. It then requires a possible large temporary buffer place the new pattern. This patch removes the alloca/malloc usage and replaces it with char_array. Checked on x86_64-linux-gnu. * posix/glob.c (glob): Remove alloca usage for onealt. --- posix/glob.c | 63 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 31 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 858b709..1892f48 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -398,44 +398,32 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* Allocate working buffer large enough for our work. Note that we have at least an opening and closing brace. */ size_t firstc; - char *alt_start; const char *p; const char *next; const char *rest; size_t rest_len; - char *onealt; - size_t pattern_len = strlen (pattern) - 1; - int alloca_onealt = glob_use_alloca (alloca_used, pattern_len); - if (alloca_onealt) - onealt = alloca_account (pattern_len, alloca_used); - else + struct char_array onealt; + + /* We know the prefix for all sub-patterns. */ + ptrdiff_t onealtlen = begin - pattern; + if (!char_array_init_str_size (&onealt, pattern, onealtlen)) { - onealt = malloc (pattern_len); - if (onealt == NULL) + if (!(flags & GLOB_APPEND)) { - if (!(flags & GLOB_APPEND)) - { - pglob->gl_pathc = 0; - pglob->gl_pathv = NULL; - } - goto err_nospace; + pglob->gl_pathc = 0; + pglob->gl_pathv = NULL; } + goto err_nospace; } - /* We know the prefix for all sub-patterns. */ - alt_start = mempcpy (onealt, pattern, begin - pattern); - /* Find the first sub-pattern and at the same time find the rest after the closing brace. */ next = next_brace_sub (begin + 1, flags); if (next == NULL) { /* It is an invalid expression. */ - illegal_brace: - if (__glibc_unlikely (!alloca_onealt)) - free (onealt); - char_array_free (&dirname); - return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob); + char_array_free (&onealt); + goto illegal_brace; } /* Now find the end of the whole brace expression. */ @@ -444,8 +432,11 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { rest = next_brace_sub (rest + 1, flags); if (rest == NULL) - /* It is an illegal expression. */ - goto illegal_brace; + { + /* It is an illegal expression. */ + char_array_free (&onealt); + goto illegal_brace; + } } /* Please note that we now can be sure the brace expression is well-formed. */ @@ -464,17 +455,24 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), int result; /* Construct the new glob expression. */ - mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len); + ptrdiff_t nextlen = next - p; + if (!char_array_replace_str_pos (&onealt, onealtlen, p, nextlen) + || !char_array_replace_str_pos (&onealt, onealtlen + nextlen, + rest, rest_len)) + { + char_array_free (&onealt); + retval = GLOB_NOSPACE; + goto out; + } - result = glob (onealt, + result = glob (char_array_str (&onealt), ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC)) | GLOB_APPEND), errfunc, pglob); /* If we got an error, return it. */ if (result && result != GLOB_NOMATCH) { - if (__glibc_unlikely (!alloca_onealt)) - free (onealt); + char_array_free (&onealt); if (!(flags & GLOB_APPEND)) { globfree (pglob); @@ -493,8 +491,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), assert (next != NULL); } - if (__glibc_unlikely (!alloca_onealt)) - free (onealt); + char_array_free (&onealt); if (pglob->gl_pathc != firstc) /* We found some entries. */ @@ -1182,6 +1179,10 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), err_nospace: char_array_free (&dirname); return GLOB_NOSPACE; + + illegal_brace: + char_array_free (&dirname); + return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob); } #if defined _LIBC && !defined glob libc_hidden_def (glob)