From patchwork Mon Feb 5 13:27:25 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: 869271 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-89965-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="gFpshpzC"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zZpLV2dn3z9t2l for ; Tue, 6 Feb 2018 00:28:01 +1100 (AEDT) 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=rvvVlxitHPrNu6A5dYLelmHL1plDNKm DZQW/5BntXKAzi7WJ4G79E5jXbPLwoHNeUXYBPXy/FYsLpXpnRV56eL8XMoQyCBX f4wmkcjtB4sOsgN7XMUanUpBBE6brYX4DiLVC7Xsc5snkNJQ4703SRoryJXwc2RG QpbQYBNonyEI= 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=C6mDyClMXS7iK5toL9RHnPotmgY=; b=gFpsh pzCncQuXref0aMoAlVyNbjAD94B5ss6twHSjjiRseu9bw7TScg1OYew95S5A/Ram 8crk3XBXeWSnOTED1kkGe+XSRPAkjR1xdtUWHGsChpUy4FKVI8Fu+1bLdLkFLarS a8YeLU+ZMR0h+DEbbKM9J79C5dLJTEkB/3oIA0= Received: (qmail 32550 invoked by alias); 5 Feb 2018 13:27:52 -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 32279 invoked by uid 89); 5 Feb 2018 13:27:49 -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, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=4677, Allocate, our X-HELO: mail-qk0-f196.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=kvukgy+q8OckB3mu7WbWUW7oiRoMqNr7ErlROk3fl28=; b=E2hOKQG6NcAEXF4aFbrLZ97j6pTWEjjbAy4JLQnO2tEvRuv+5l+XGnu6WFy6XR8C6l lF4J4vnX0nUPFF1hcsO4zdfbrPN+pnZNhbSXeAe6BnmqHaQTALdOpoPHlTrC3rU5XL11 1wQ7Fg7juEozgetnhKbpJvDX7/++sWDa2B0HDRebV89ufNaGkstW9I0Jd3f52ZdSkCc4 af5z0BGmEXy38l8IW/dhat6MsPwCDlJFI+TfSuv3vwxlMsSKVKVlPY0kJbchF17p9iGX Zp7RWFkScoepdjR8tSWCmO2kCibRikr5MJSUSVKu8O1uunDEIKCvOCV2MwxFhYPXEJA8 EJWQ== X-Gm-Message-State: APf1xPBGf/Q9ivy/l1kkdwRo3EkFnZUT7yHpd8FDNSoO9OUUkAQvFHPF dWa0xV/1hb5I/p4JK5xDSbvdL4TJwe0= X-Google-Smtp-Source: AH8x225LJbM+Bln6KRJESbZDfb4DjrCeWJXG0yzB3c2rdhFlXkIbaJ/wBtCk5LRyFrFFxZXFrOagdw== X-Received: by 10.55.40.98 with SMTP id o95mr23542561qkh.216.1517837266079; Mon, 05 Feb 2018 05:27:46 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 03/12] posix: Remove alloca usage for GLOB_BRACE on glob Date: Mon, 5 Feb 2018 11:27:25 -0200 Message-Id: <1517837254-19399-4-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> 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(-) diff --git a/posix/glob.c b/posix/glob.c index 4814d67..3e36eeb 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. */