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: 126876 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp1971167ljc; Mon, 5 Feb 2018 05:28:04 -0800 (PST) X-Google-Smtp-Source: AH8x225OTuoxzfOkkO0bTHfIT6rbY3D7hRp+yywhTpgyslXTjj1yorQHVXbmeS9EBPVKZL/PagBs X-Received: by 10.98.102.212 with SMTP id s81mr6874322pfj.209.1517837284471; Mon, 05 Feb 2018 05:28:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517837284; cv=none; d=google.com; s=arc-20160816; b=P0vNK7wDujOfw/gZ2MsoTskI7evTwanprJVrwUiLkH6nbK8JoY9oDU4l0beP20Qe8q KQr/5qH4XhVu1C7g4Thv6/Wa/lDomRBtGomJgmJLBL5bwoI+l5xJjW7DdKLOPkFx1AxY iFaDwyM1pzEBut4M+Zv9sbmgtb9eatOBt7LyE7xsJrlTY2JBjSVSeXT1+BSt8URqfZ6S kPIfM/x2eGkOJVJQJELh3pfC8ouW7wRWG2YDIMNk00HF7Pfya66Z35lRIgr4KKk388dE Fpf0x2YniP+8cifiEP7PwzX53XItDZyiLQartZblrVtniyTumkd6GSIOrujHQyvFKmGF Q5qQ== 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=kvukgy+q8OckB3mu7WbWUW7oiRoMqNr7ErlROk3fl28=; b=SKMMZonDeaKKP9OxGBopii/YSk8Z0g4VazueTq0JOx2XjwuRFc8mrgJb+RV5bG+/w6 3F3EGfeV6zVL3rq8Dd2Gs/quZykLIJxx/Sw1ue8LJP+0r5vo+zcLUmDCcSae5P/+C8ue COY/wzLyqePoElrPzvSSXA7ULlVNk8QWgJMKUslKBcTY1tp0BGG1BFFENCjZNQrWZhe6 gsmEsofHOCxyoi6ZlkXqQKQhv1aRENO4QHge1ZrRkR8Ny2aFV3rhO++8+/+N6Zd9nKmf 2F1WtZjx3SSMs1Wu2shcFbc8lStjCNhZq4bYy+XtdrrHtjyZyGJFJZDAK7b7t4+g9eoM eSIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=G3idYXaK; spf=pass (google.com: domain of libc-alpha-return-89965-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89965-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 32-v6si5018482pld.447.2018.02.05.05.28.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Feb 2018 05:28:04 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-89965-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=G3idYXaK; spf=pass (google.com: domain of libc-alpha-return-89965-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89965-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=Hw//dQ4WOEHxVefURutQVlNmVR0wRY0 It1FtHbxj59/FNxmRQhJR4ZyBsX4Vm/oSBsHE0YNnrVzH4WT+wxCZQAGq75I9NwO IFK9+vLCgp4XZdBS5R+7aovAVsuVsMcN5JAok/H7CFZ0XhEHLfK0dkKY8yCkGdvu 5sFerDfTR4LY= 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=G3idY XaKGL0R/E3GZ0TTgjrV0I/rkzDtgKTGVjcU+bfEY0w1xG2hazn7WoMwJAkmZqRru KPhf7BY9HjVSfTkUH9CvBiAfv5/eoqaao3lrN3hq/I9FeLcgcwgaOERQSqnea2p/ 5UCDquGojVuDzcemO3SEAg1E/3rfx+DWXtKJ8o= 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-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(-) -- 2.7.4 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. */