From patchwork Mon Jan 4 20:25:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 41636 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 086063892446; Mon, 4 Jan 2021 20:25:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 086063892446 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1609791940; bh=XNi12vi020oM2BwtwWXnJvUP8f28wlD6V67LaBgOGw0=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=KTfa1++s4O/uxXBK1CGTIYcaQSPtCJxPdEMSyw5+NeqEVw4qLosboIBpqu9/fOOL1 cEQCZWamtT1HiHLP2URyReV3RT8rOqxFzre2HJlSZZoXKGhz6QktbnaAJp1BCu5+VI fcxQ8nV3jd6U/h9JaHUeQGwe5W8VY0wMs5aRWFPk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf35.google.com (mail-qv1-xf35.google.com [IPv6:2607:f8b0:4864:20::f35]) by sourceware.org (Postfix) with ESMTPS id 4C8AD3861875 for ; Mon, 4 Jan 2021 20:25:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 4C8AD3861875 Received: by mail-qv1-xf35.google.com with SMTP id az16so13644666qvb.5 for ; Mon, 04 Jan 2021 12:25:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XNi12vi020oM2BwtwWXnJvUP8f28wlD6V67LaBgOGw0=; b=uUawO2fjA1CafdYj1sl1irwt5210vvR9HKBt2ny+fKS4tezg+zbsf/3DMdUlJwtFUK hqwH9cutoPHBr1em0sJNdr5osMPfJqkXJ7TiAdvzkm128AazLFDs49+POC7BUHBlQjxX zx3nNlsESKyRxiDG0RuKktY3KL+qBKxDZQPrNA/CHUIH5bgjFhthbqNXYU02VjnlK2tk MtmndHwmbMyElxxD8W6tozQswsY7Gz6f0fecMV2xFJvzfJQp5NRUvJCIpvo04TnM3grY ZglauZVn7xEfZNPmOY5UzvC1bbj6RN9hy3RrO6dhQzMXBBlMHVDiRDT7Qbr8W2KABi/g JdMA== X-Gm-Message-State: AOAM530lgNam/1twPboFpeojBWD5zzlfoyig43fHHzyfiH4ZrjfOEUvN JA7eP8N5LzzATBLdsecWUv7JUHNZb70Dxg== X-Google-Smtp-Source: ABdhPJxFVji/LaHyI+hQN6L0L/OIL+315ecqFatZR0zWRaXa+zaBDkKQ/PF19IeyASWdlxP6bU2ylw== X-Received: by 2002:a0c:fe90:: with SMTP id d16mr78957649qvs.13.1609791935427; Mon, 04 Jan 2021 12:25:35 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id b4sm36292928qtb.33.2021.01.04.12.25.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jan 2021 12:25:34 -0800 (PST) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH 2/2] posix: Remove alloca usage for internal fnmatch implementation Date: Mon, 4 Jan 2021 17:25:28 -0300 Message-Id: <20210104202528.1228255-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210104202528.1228255-1-adhemerval.zanella@linaro.org> References: <20210104202528.1228255-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: bug-gnulib@gnu.org Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" This patch replaces the internal fnmatch pattern list generation to use a dynamic array. Checked on x86_64-linux-gnu. --- posix/fnmatch.c | 24 +----- posix/fnmatch_loop.c | 190 +++++++++++++++++++------------------------ 2 files changed, 87 insertions(+), 127 deletions(-) diff --git a/posix/fnmatch.c b/posix/fnmatch.c index ac254fc9ac..2a6186b594 100644 --- a/posix/fnmatch.c +++ b/posix/fnmatch.c @@ -31,9 +31,6 @@ #include #include #include -#if defined _LIBC || HAVE_ALLOCA -# include -#endif #include #include #include @@ -87,22 +84,6 @@ typedef ptrdiff_t idx_t; #define NO_LEADING_PERIOD(flags) \ ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD)) -#ifndef _LIBC -# if HAVE_ALLOCA -/* The OS usually guarantees only one guard page at the bottom of the stack, - and a page size can be as small as 4096 bytes. So we cannot safely - allocate anything larger than 4096 bytes. Also care for the possibility - of a few compiler-allocated temporary stack slots. */ -# define __libc_use_alloca(n) ((n) < 4032) -# else -/* Just use malloc. */ -# define __libc_use_alloca(n) false -# undef alloca -# define alloca(n) malloc (n) -# endif -# define alloca_account(size, avar) ((avar) += (size), alloca (size)) -#endif - /* Provide support for user-defined character classes, based on the functions from ISO C 90 amendment 1. */ #ifdef CHARCLASS_NAME_MAX @@ -293,8 +274,7 @@ fnmatch (const char *pattern, const char *string, int flags) int res = internal_fnwmatch (wpattern.data, wstring.data, (wchar_t *) wstring.data + n, - flags & FNM_PERIOD, flags, NULL, - false); + flags & FNM_PERIOD, flags, NULL); scratch_buffer_free (&wstring); scratch_buffer_free (&wpattern); @@ -303,7 +283,7 @@ fnmatch (const char *pattern, const char *string, int flags) } return internal_fnmatch (pattern, string, string + strlen (string), - flags & FNM_PERIOD, flags, NULL, 0); + flags & FNM_PERIOD, flags, NULL); } #undef fnmatch diff --git a/posix/fnmatch_loop.c b/posix/fnmatch_loop.c index 7f938af590..69f78f0fd8 100644 --- a/posix/fnmatch_loop.c +++ b/posix/fnmatch_loop.c @@ -30,15 +30,14 @@ struct STRUCT it matches, nonzero if not. */ static int FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end, bool no_leading_period, int flags, - struct STRUCT *ends, size_t alloca_used); + struct STRUCT *ends); static int EXT (INT opt, const CHAR *pattern, const CHAR *string, - const CHAR *string_end, bool no_leading_period, int flags, - size_t alloca_used); + const CHAR *string_end, bool no_leading_period, int flags); static const CHAR *END (const CHAR *patternp); static int FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end, - bool no_leading_period, int flags, struct STRUCT *ends, size_t alloca_used) + bool no_leading_period, int flags, struct STRUCT *ends) { const CHAR *p = pattern, *n = string; UCHAR c; @@ -62,8 +61,7 @@ FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end, case L_('?'): if (__glibc_unlikely (flags & FNM_EXTMATCH) && *p == '(') { - int res = EXT (c, p, n, string_end, no_leading_period, - flags, alloca_used); + int res = EXT (c, p, n, string_end, no_leading_period, flags); if (res != -1) return res; } @@ -92,8 +90,7 @@ FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end, case L_('*'): if (__glibc_unlikely (flags & FNM_EXTMATCH) && *p == '(') { - int res = EXT (c, p, n, string_end, no_leading_period, - flags, alloca_used); + int res = EXT (c, p, n, string_end, no_leading_period, flags); if (res != -1) return res; } @@ -182,7 +179,7 @@ FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end, for (--p; n < endp; ++n, no_leading_period = false) if (FCT (p, n, string_end, no_leading_period, flags2, - &end, alloca_used) == 0) + &end) == 0) goto found; } else if (c == L_('/') && (flags & FNM_FILE_NAME)) @@ -191,7 +188,7 @@ FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end, ++n; if (n < string_end && *n == L_('/') && (FCT (p, n + 1, string_end, flags & FNM_PERIOD, flags, - NULL, alloca_used) == 0)) + NULL) == 0)) return 0; } else @@ -205,7 +202,7 @@ FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end, for (--p; n < endp; ++n, no_leading_period = false) if (FOLD ((UCHAR) *n) == c && (FCT (p, n, string_end, no_leading_period, flags2, - &end, alloca_used) == 0)) + &end) == 0)) { found: if (end.pattern == NULL) @@ -892,8 +889,7 @@ FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end, case L_('!'): if (__glibc_unlikely (flags & FNM_EXTMATCH) && *p == '(') { - int res = EXT (c, p, n, string_end, no_leading_period, flags, - alloca_used); + int res = EXT (c, p, n, string_end, no_leading_period, flags); if (res != -1) return res; } @@ -972,26 +968,37 @@ END (const CHAR *pattern) return p + 1; } +#if WIDE_CHAR_VERSION +# define PATTERN_PREFIX pattern_list +#else +# define PATTERN_PREFIX wpattern_list +#endif + +#define PASTE(a,b) PASTE1(a,b) +#define PASTE1(a,b) a##b + +#define DYNARRAY_STRUCT PATTERN_PREFIX +#define DYNARRAY_ELEMENT_FREE(ptr) free (*ptr) +#define DYNARRAY_ELEMENT CHAR * +#define DYNARRAY_PREFIX PASTE(PATTERN_PREFIX,_) +#define DYNARRAY_INITIAL_SIZE 8 +#include static int EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end, - bool no_leading_period, int flags, size_t alloca_used) + bool no_leading_period, int flags) { const CHAR *startp; ptrdiff_t level; - struct patternlist - { - struct patternlist *next; - CHAR malloced; - CHAR str __flexarr; - } *list = NULL; - struct patternlist **lastp = &list; + struct PATTERN_PREFIX list; size_t pattern_len = STRLEN (pattern); - bool any_malloced = false; + size_t pattern_i = 0; const CHAR *p; const CHAR *rs; int retval = 0; + PASTE (PATTERN_PREFIX, _init) (&list); + /* Parse the pattern. Store the individual parts in the list. */ level = 0; for (startp = p = pattern + 1; level >= 0; ++p) @@ -1027,74 +1034,48 @@ EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end, || *p == L_('!')) && p[1] == L_('(')) /* Remember the nesting level. */ ++level; - else if (*p == L_(')')) - { - if (level-- == 0) - { - /* This means we found the end of the pattern. */ -#define NEW_PATTERN \ - struct patternlist *newp; \ - size_t plen = (opt == L_('?') || opt == L_('@') \ - ? pattern_len : (p - startp + 1UL)); \ - idx_t slen = FLEXSIZEOF (struct patternlist, str, 0); \ - idx_t new_used = alloca_used + slen; \ - idx_t plensize; \ - if (INT_MULTIPLY_WRAPV (plen, sizeof (CHAR), &plensize) \ - || INT_ADD_WRAPV (new_used, plensize, &new_used)) \ - { \ - retval = -2; \ - goto out; \ - } \ - slen += plensize; \ - bool malloced = ! __libc_use_alloca (new_used); \ - if (__glibc_unlikely (malloced)) \ - { \ - newp = malloc (slen); \ - if (newp == NULL) \ - { \ - retval = -2; \ - goto out; \ - } \ - any_malloced = true; \ - } \ - else \ - newp = alloca_account (slen, alloca_used); \ - newp->next = NULL; \ - newp->malloced = malloced; \ - *((CHAR *) MEMPCPY (newp->str, startp, p - startp)) = L_('\0'); \ - *lastp = newp; \ - lastp = &newp->next - NEW_PATTERN; - } - } - else if (*p == L_('|')) + else if (*p == L_(')') || *p == L_('|')) { if (level == 0) { - NEW_PATTERN; - startp = p + 1; + size_t slen = opt == L_('?') || opt == L_('@') + ? pattern_len : p - startp + 1; + CHAR *newp = malloc (slen * sizeof (CHAR)); + if (newp != NULL) + { + *((CHAR *) MEMPCPY (newp, startp, p - startp)) = L_('\0'); + PASTE (PATTERN_PREFIX,_add) (&list, newp); + } + if (newp == NULL || PASTE (PATTERN_PREFIX, _has_failed) (&list)) + { + retval = -2; + goto out; + } + + if (*p == L_('|')) + startp = p + 1; } + if (*p == L_(')')) + level--; } - assert (list != NULL); assert (p[-1] == L_(')')); -#undef NEW_PATTERN switch (opt) { case L_('*'): - if (FCT (p, string, string_end, no_leading_period, flags, NULL, - alloca_used) == 0) + if (FCT (p, string, string_end, no_leading_period, flags, NULL) == 0) goto success; FALLTHROUGH; case L_('+'): - do + for (; pattern_i < PASTE (PATTERN_PREFIX, _size)(&list); pattern_i++) { for (rs = string; rs <= string_end; ++rs) /* First match the prefix with the current pattern with the current pattern. */ - if (FCT (list->str, string, rs, no_leading_period, + if (FCT (*PASTE (PATTERN_PREFIX, _at) (&list, pattern_i), string, + rs, no_leading_period, flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD, - NULL, alloca_used) == 0 + NULL) == 0 /* This was successful. Now match the rest with the rest of the pattern. */ && (FCT (p, rs, string_end, @@ -1102,7 +1083,7 @@ EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end, ? no_leading_period : rs[-1] == '/' && NO_LEADING_PERIOD (flags), flags & FNM_FILE_NAME - ? flags : flags & ~FNM_PERIOD, NULL, alloca_used) == 0 + ? flags : flags & ~FNM_PERIOD, NULL) == 0 /* This didn't work. Try the whole pattern. */ || (rs != string && FCT (pattern - 1, rs, string_end, @@ -1110,35 +1091,33 @@ EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end, ? no_leading_period : rs[-1] == '/' && NO_LEADING_PERIOD (flags), flags & FNM_FILE_NAME - ? flags : flags & ~FNM_PERIOD, NULL, - alloca_used) == 0))) + ? flags : flags & ~FNM_PERIOD, NULL) == 0))) /* It worked. Signal success. */ goto success; } - while ((list = list->next) != NULL); /* None of the patterns lead to a match. */ retval = FNM_NOMATCH; break; case L_('?'): - if (FCT (p, string, string_end, no_leading_period, flags, NULL, - alloca_used) == 0) + if (FCT (p, string, string_end, no_leading_period, flags, NULL) == 0) goto success; FALLTHROUGH; case L_('@'): - do - /* I cannot believe it but 'strcat' is actually acceptable - here. Match the entire string with the prefix from the - pattern list and the rest of the pattern following the - pattern list. */ - if (FCT (STRCAT (list->str, p), string, string_end, - no_leading_period, - flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD, - NULL, alloca_used) == 0) - /* It worked. Signal success. */ - goto success; - while ((list = list->next) != NULL); + for (; pattern_i < PASTE (PATTERN_PREFIX, _size) (&list); pattern_i++) + { + /* I cannot believe it but `strcat' is actually acceptable + here. Match the entire string with the prefix from the + pattern list and the rest of the pattern following the + pattern list. */ + if (FCT (STRCAT (*PASTE (PATTERN_PREFIX, _at) (&list, pattern_i), p), + string, string_end, no_leading_period, + flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD, + NULL) == 0) + /* It worked. Signal success. */ + goto success; + } /* None of the patterns lead to a match. */ retval = FNM_NOMATCH; @@ -1147,22 +1126,27 @@ EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end, case L_('!'): for (rs = string; rs <= string_end; ++rs) { - struct patternlist *runp; + size_t runp_i; - for (runp = list; runp != NULL; runp = runp->next) - if (FCT (runp->str, string, rs, no_leading_period, - flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD, - NULL, alloca_used) == 0) + for (runp_i = pattern_i; + runp_i != PASTE (PATTERN_PREFIX, _size) (&list); + runp_i++) + { + if (FCT (*PASTE (PATTERN_PREFIX, _at) (&list, runp_i), string, rs, + no_leading_period, + flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD, + NULL) == 0) break; + } /* If none of the patterns matched see whether the rest does. */ - if (runp == NULL + if (runp_i == PASTE (PATTERN_PREFIX, _size) (&list) && (FCT (p, rs, string_end, rs == string ? no_leading_period : rs[-1] == '/' && NO_LEADING_PERIOD (flags), flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD, - NULL, alloca_used) == 0)) + NULL) == 0)) /* This is successful. */ goto success; } @@ -1180,18 +1164,14 @@ EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end, success: out: - if (any_malloced) - while (list != NULL) - { - struct patternlist *old = list; - list = list->next; - if (old->malloced) - free (old); - } + PASTE (PATTERN_PREFIX, _free) (&list); return retval; } +#undef PATTERN_PREFIX +#undef PASTE +#undef PASTE1 #undef FOLD #undef CHAR