From patchwork Thu Jun 8 21:13:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 103416 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632908qgd; Thu, 8 Jun 2017 14:16:09 -0700 (PDT) X-Received: by 10.98.63.10 with SMTP id m10mr28301220pfa.19.1496956569202; Thu, 08 Jun 2017 14:16:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956569; cv=none; d=google.com; s=arc-20160816; b=cQ4/zU8l908i4angP5ZGIDSBAmS6O/Df3cO/EQiJ5ajYBVCKLTh7sTlA5Jeqb5djtG Txzvxs53Eh1VtnqzxLAXZU7d8A/SuggEVfUhFGgCSTwWL//J2gozrkB/8oRuiRBOeg8k HOscz1DWRl1ncCYh02vMZx6Ao79Ox4zJ+GQ8vZGr6QZYvG1yxvcC0z2sOHRdVGrdfdXa YBo/yXndt/HNbdWTd/jQt/zUHSRKZFQ8j4XXW9+HNSZwXd5PkoNTg8FTFkeRovin9L/C u5LKtOKGFRRZBdeXP3M3l4bFGWDRxSLDxPwxHvCWcNQoXYG3YzHeAKYzR3qJDKqDIBc5 CVog== 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=9PUnokyi+MFN4VfrfpWTAGJSbghDlfuO3gqqLmOhmAA=; b=oTbzcvyszgZYDa+Z57MA75G6XckGR2rWC21qHP9RKAoBE9ud3gFgZBPGExC46L0I7H Z4WfQ39MtEv6f0zv558K1jp/0vEbtwaqHtB17iN1L/rN8tcEdO3H5SKb8dg33QqVCIm/ SxfIkSSMSIG+4GYuh9iC+zN3wbt7NdMuRGGUuujbL80YNC1kV7mOUfoS2f7qZFPLF36S Nkij27p6Ml3+zm3mNcCW1+aAlxGDRf2DUJg5VBAw8pORlqyik/PSBELUvUBLh9toEodc YhxEZfTXTrgYCxvQlp0adx+Gb99Gb+U4KkFVeJoKRQ0Qfgy4mEDTlixxBgIygbymk2zG iRXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80210-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80210-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 l1si5131592plb.129.2017.06.08.14.16.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:16:09 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80210-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; spf=pass (google.com: domain of libc-alpha-return-80210-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80210-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=fAcuv396v092LpyexZxeAQaLIc/F+G0 ZN2gpnQ0nrtk/RzrxpPd4cCaiQTD1Z65MpKRGVOspweBBmnMaJrusoACt2RIR9Dx u/LQlcQh6LG334e7nkG7tRtbLq4b6LCyBTe+WdBHsRBjI/9yHiNsFslSaSOOCQyr smew+KezRWj0= 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=IKYxUdg5wAA/EnLRnpu5Hda712c=; b=YeLvG gVOrOwhNUMLcnVYA8XyGSXUb3LrVFRu1yW2q/PXcoZSGmaScIMG5/Qj/M4KqEpQ4 4vTKYv/oHmk4OGOr652i3IHfyEy9xm9VjFZ8O+pOA8CI9VVQi1rt3OcvYWhSxgMV Y/QNoBgw7ZmfxLzl9eZYrQ+1Is3PQEgHbKwnGE= Received: (qmail 39886 invoked by alias); 8 Jun 2017 21:14:10 -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 39722 invoked by uid 89); 8 Jun 2017 21:14:09 -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= 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=9PUnokyi+MFN4VfrfpWTAGJSbghDlfuO3gqqLmOhmAA=; b=Xsi+ApOpCUIXbpekIHvUGgzSLlUBB4zIo7TfBpi9uEieiuNFw/jGTfNVzWxhAh8SL9 kjegCOjjlye0KBULeoHa1sz6mwEDcoqtXO5xbI1NH/8NoniCgroaiVjFWvJs57sJWefP SccgK71I4aSPnLBpGhXVBJlCYheaadIUisfUkAaglDj8RwyPDed7IrakE3BX1fkAVGNr eE2VmEL8+WJ9UGFR13LCRAiLcEVMYnkQgWfGeIYeibWViP8wsGQMkWD5k5tZPCj9V5v4 hyllQCN/0HMZLyHlQQZA9K8mLs5zE8jA/Pk4EJbJnYPqTpOVSb8SZt2v8VdAb6Tji7qB GjAQ== X-Gm-Message-State: AODbwcDnrPbU1HndTx4mLwM4VvYb5MhLP4jtXJKhqvzceItW4hvmkuA9 N7dG7HqsMKFxiQeN6Wp9kw== X-Received: by 10.237.44.7 with SMTP id f7mr21278321qtd.142.1496956449835; Thu, 08 Jun 2017 14:14:09 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 11/17] posix: Remove alloca usage for GLOB_BRACE on glob Date: Thu, 8 Jun 2017 18:13:25 -0300 Message-Id: <1496956411-25594-12-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> 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 9e3050a..c83feb4 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -397,44 +397,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. */ @@ -443,8 +431,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. */ @@ -463,17 +454,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); @@ -492,8 +490,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. */ @@ -1180,6 +1177,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)