From patchwork Tue Nov 21 13:55: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: 119353 Delivered-To: patch@linaro.org Received: by 10.80.225.132 with SMTP id k4csp5124340edl; Tue, 21 Nov 2017 05:56:03 -0800 (PST) X-Google-Smtp-Source: AGs4zMYyUjssAgsnUFTqSX7Td8290CeTvPLh9YeMHvgNce+XaHCFdMAsd0wGjdsSL1BqGfQiObgm X-Received: by 10.98.75.77 with SMTP id y74mr15324208pfa.78.1511272563414; Tue, 21 Nov 2017 05:56:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511272563; cv=none; d=google.com; s=arc-20160816; b=gUiXRxv0ZrzEgtLrwSja6RYQ39JbJnVf3rGpTypG4vih7LL6M952BhQn+VcWZDMdAb Y39rkGIxzVZGSFpkmW/PLz6Viay26Yiqe15lItHNf64029yZUAR4CJVeq+3+WMpCQFHb kSAP4Hzs2h2VBMTm1WGFuEXE8Mocjl291jbmf50tiEvrmazYPHaVPswRESeRNlvM4TiC RGFTwkq7wT+4d2AaNASEGUNOxcG7WLag8LFXUjMSy/hd3XWdL/ZKerDzjdhn/NUqzeQK TBO2evNQJfEe3jOEeTnm0IAx2usT6EMjxvgDfC2QdK8mewdMCuuze/2tP0WltRLDEe+y p5iA== 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=PbDK1ZdOvAaq7/7jMNxStveY9YzGesPDcIH0wa5euRg=; b=gZPgH7VoqHx6CEwKAZCyGQQrzSpLCeADFsljMxhzAP7oFZwJ2e7mrm7nSWK736GF3q iFhEaW063rBccedzDGd/KBcoKk8A5TR6VQgYMLYUqm3ZAeSJzeCCytnObONOa07ZF5Bp lksJ9pvvHpJzpaGzbz1vJUcwrCRhSCi7l+Psl1fDcT41n0/5iQesC7BYwM0Y6acOMrsR sEpW919cHU767Kg94WkD1vwdSk7T92RTnIKgt+QpkyIva/8pkZvNCLw2bVjhZTP2qhgO 7YkkayNMKW5k10yt5zpxm1cALvap1s2u0WDujwq5HdZYnkdApXzGbzb7UXm3aIJ2IkCz K7xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=Az8ql6w0; spf=pass (google.com: domain of libc-alpha-return-87357-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-87357-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 76si5418689pfy.58.2017.11.21.05.56.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Nov 2017 05:56:03 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-87357-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=Az8ql6w0; spf=pass (google.com: domain of libc-alpha-return-87357-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-87357-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=TjYLq1I3MHqpvvY3/nJOjeXcNMgen1x MSLoHjkLND4u8tvO20iKfNMhMY4uPN1luQCmKugzexg4SOrWQ0wOYfjLzFG5efvg hkcEA3PT8/7BHdFxxp8yGOOPIW8+EaaamRAeGXOoHvaDoWJDrBpB6UEA/TY6M618 cUqtUOrGXAS0= 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=YtzJei3/pAZD9a/pXSqepQKBSwA=; b=Az8ql 6w0iWjZemH8wik8/82wiMvywBgAW/IGoogxUUn6Ga5wPLMDo7noxFIHhO6YvET7m p+raYsFAZ94hBRY6ktrMjvO6Sgpa914+oO56TVAdbxinXHS/QHMoET6FEZ9VN6Dr Uv5/s7aOPnAsNkn2LhS77fdk50V3Ef6jNpY7LY= Received: (qmail 44146 invoked by alias); 21 Nov 2017 13:55:46 -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 44069 invoked by uid 89); 21 Nov 2017 13:55:45 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.6 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=brace X-HELO: mail-qk0-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=PbDK1ZdOvAaq7/7jMNxStveY9YzGesPDcIH0wa5euRg=; b=IvZkFAuSxi04ZQuY4SEPM+ywf4UJGzvIIfzVql2je4wHujhP2pgngfm6AgXqa2NrBZ fLHJTQtdbQZYdxbLyWTzzoN555/LR6CQQkphRpsv4HTvH7UF5KWCyOGk5GdnlPYuJIUW crt2SpdJBRY8jIBXl4xiWT7esNLAMvH9UTMtkWkcVC4tM5RWkeBdPLyhSDc4qJcFiuY4 0L2AFh4na/uUdMbfXDRk9evAOx4fj79ifHTuHeX2Rbyb8ih7nPxeGuJigJjUCfaeqOk1 6RPNy2yz5HrfNJGZ6grOdY+fAs9BSnMC7uKip5DH+SkeTB17rPAnCwhV5jI9qBdxtQUi W/bw== X-Gm-Message-State: AJaThX77zPQV+zXFUeeOaDuiEnhcLombsNVQ7qoHZGDVOyY76h9SPhUy 8bAS9pmsjd6pgpoQXTbfZ35kwPcVSZE= X-Received: by 10.55.190.135 with SMTP id o129mr347074qkf.282.1511272541885; Tue, 21 Nov 2017 05:55:41 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 3/8] posix: Remove alloca usage for GLOB_BRACE on glob Date: Tue, 21 Nov 2017 11:55:25 -0200 Message-Id: <1511272530-10936-4-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> 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. Signed-off-by: Adhemerval Zanella --- ChangeLog | 2 ++ posix/glob.c | 53 +++++++++++++++++++++++++++++------------------------ 2 files changed, 31 insertions(+), 24 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 7a89d2f..7c0df0b 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -372,25 +372,23 @@ __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 - { - onealt = malloc (pattern_len); - if (onealt == NULL) - goto err_nospace; - } + struct char_array onealt; /* We know the prefix for all sub-patterns. */ - alt_start = mempcpy (onealt, pattern, begin - pattern); + ptrdiff_t onealtlen = begin - pattern; + if (!char_array_init_str_size (&onealt, pattern, onealtlen)) + { + if (!(flags & GLOB_APPEND)) + { + pglob->gl_pathc = 0; + pglob->gl_pathv = NULL; + } + goto err_nospace; + } /* Find the first sub-pattern and at the same time find the rest after the closing brace. */ @@ -398,9 +396,7 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (next == NULL) { /* It is an invalid expression. */ - illegal_brace: - if (__glibc_unlikely (!alloca_onealt)) - free (onealt); + char_array_free (&onealt); flags &= ~GLOB_BRACE; goto no_brace; } @@ -411,8 +407,12 @@ __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); + flags &= ~GLOB_BRACE; + goto no_brace; + } } /* Please note that we now can be sure the brace expression is well-formed. */ @@ -431,9 +431,16 @@ __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); + goto err_nospace; + } - result = __glob (onealt, + result = __glob (char_array_str (&onealt), ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC)) | GLOB_APPEND), errfunc, pglob); @@ -441,8 +448,7 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* 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); @@ -461,8 +467,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. */