From patchwork Mon Feb 5 13:27:32 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: 126885 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp1972231ljc; Mon, 5 Feb 2018 05:29:29 -0800 (PST) X-Google-Smtp-Source: AH8x226ZTqH797ZxIq9M8hUy8XCeNKTa+QoHiiboxH1QDw48xt65uOyhXqEA2B+UXrGD6zm1TftZ X-Received: by 2002:a17:902:4181:: with SMTP id f1-v6mr44068927pld.59.1517837369675; Mon, 05 Feb 2018 05:29:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517837369; cv=none; d=google.com; s=arc-20160816; b=JPK/ZqoWX/4HKzo3GrkVP//C91VkUpNak3rkuYvqtQc2jNizbnUXPjObx+dnETWRlf ygzkytdYFUQ2WQfXtdg86KRv8Rdk6hToy/dAZ+aILhiW9yW4XEPOAwdVCI1wMkgzIgHf 9imnlUjH66Q/fRQp1x6RFabSJeU4ikf4E+0OHvVlMz9NzKWEPdmQjo627EWSS7lmFjxl UJNZYg36gse+GrjQNK5PqAC/Ha9ruz1jCpCTvR/phiRS00zbzGrYivpBBl1ArJQ3WHKd xBPEap26m7PiN2lYNWHk/UD+hU4k7m+FqnmDyTMzIAoHQmV7CdjKnlBnZf+CE9Y16wRl MlMw== 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=pO5YmZXZCF3nCQcBtKV/l7o12KYqRoZQdn90q4+ckw4=; b=AlN5zDoJtaqt1o/Ao7nDOcowutX81gLNcyJWoLNez78bu/+09vlReFF8Mio8CitVqF roKROIr4lnj55SSFYBS5fdwma42zg45w/UxI7PcpPYveu1T+fjDSGo2NkERUvpEjDyD4 1MwQTU8SH84n2HnUu/bMK/RJn1spOFBzY3dxJSzHWf02/1e/PeQ7c2DsnOdo0C/6YVzx oY8uWoQmIHAN7Cq3vVBm5VHk26kF1fYYEcNaIyj7emxehdDW2tWVfDIJvnQdqyxJxxKP 5I3Gh6IXHeXOlXApi2qztaD5FE5pPdIv7w9g/1mZINCOrxTh5cqjXNgKsNDqFpmL+QBl v+QA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=tufCOXJb; spf=pass (google.com: domain of libc-alpha-return-89974-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89974-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 o1-v6si4788194plb.686.2018.02.05.05.29.29 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Feb 2018 05:29:29 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-89974-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=tufCOXJb; spf=pass (google.com: domain of libc-alpha-return-89974-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89974-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=S4YQcmyt8atI9BqGEQ81YRwEXrR7Vh+ rHkzCrX69V9txsgVOOSUsOg8EWsfC6RSPG/Ys3XnwXJGJanUf6BawER1bi2wY0Z7 NlVuqKlvU+Lc6trQ69NUHW7FT3RUlcF4B5Gdbwj+RmbCeUaR6jZLn6NohqCkdRNO hjINL2zs7CF8= 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=cXBJZNshAgZsd0pdO4vw8tbDZxs=; b=tufCO XJbKisbaUvAwF8NPrzKkukw78G6CtUPqu5GTX2BZxWLz16Dm126XbP0fxf8tkrYw 1+BjwZGxApTWmxf0rO75Dnrt08bzWE6Db6hjJvH9bPTwqELx0KnidHJ4aXgP44bS dUaSpKll+JuTPo3p8h5V4A9f9WgZSClYgAUisA= Received: (qmail 33993 invoked by alias); 5 Feb 2018 13:28:00 -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 33913 invoked by uid 89); 5 Feb 2018 13:27:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.8 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= X-HELO: mail-qt0-f195.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=pO5YmZXZCF3nCQcBtKV/l7o12KYqRoZQdn90q4+ckw4=; b=hFbWBEShaP6oEexarc/aPm+yXczzQSv4ag3S9MUYqiLJwaNmH7wkY7L2uQOMvYx3KY vgcF9g8+R9mSpwhR0CpiXWcbBgevkRVgvJovmwh5H1sevbyumCE2VmAG2AP2pPO06ZhJ yY1N6Up/lh1gfoDzO2OMYkUrbSoCrrDRBhTX61MvqCtwxxkKaQGc+xEoJtXP+0c48gg7 4qQ4LfcvajAhhZX+LfTSbayc/hz3RCwuLGptc2azQ69KZIiXItEwqAa71fWOSmvk/BYj bMMR5/PPwSpuG1J/pNrmIfxnk4i2MlJdEIxZ3fZ8D9b18Gcg+5ZTtO2OOC4EEmtUr5Eu kNgA== X-Gm-Message-State: APf1xPBucVDyILbwThFEkghRxV+xc9xZkSJ3pe0bS8umuIiypU+bx4cY BARz/WW/Ogx6FXj5U6wTdmR6Uqatpo8= X-Received: by 10.200.53.221 with SMTP id l29mr2621846qtb.273.1517837275322; Mon, 05 Feb 2018 05:27:55 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 10/12] posix: Replace alloca usage with scratch_buffer for fnmatch Date: Mon, 5 Feb 2018 11:27:32 -0200 Message-Id: <1517837254-19399-11-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> Checked on x86_64-linux-gnu. * posix/fnmatch.c (fnmatch): Use scratch_buffer instead of alloca. Signed-off-by: Adhemerval Zanella --- ChangeLog | 5 ++ posix/fnmatch.c | 140 ++++++++++---------------------------------------------- 2 files changed, 29 insertions(+), 116 deletions(-) -- 2.7.4 diff --git a/posix/fnmatch.c b/posix/fnmatch.c index a9b7626..4be7327 100644 --- a/posix/fnmatch.c +++ b/posix/fnmatch.c @@ -34,11 +34,7 @@ # include #endif -#ifdef _LIBC -# include -#else -# define alloca_account(size., var) alloca (size) -#endif +#include /* For platform which support the ISO C amendement 1 functionality we support user defined character classes. */ @@ -315,128 +311,40 @@ is_char_class (const wchar_t *wcs) # include "fnmatch_loop.c" # endif - int fnmatch (const char *pattern, const char *string, int flags) { # if HANDLE_MULTIBYTE - if (__builtin_expect (MB_CUR_MAX, 1) != 1) + if (__glibc_unlikely (MB_CUR_MAX != 1)) { - mbstate_t ps; - size_t n; - const char *p; - wchar_t *wpattern_malloc = NULL; - wchar_t *wpattern; - wchar_t *wstring_malloc = NULL; - wchar_t *wstring; - size_t alloca_used = 0; + mbstate_t ps = { 0 }; - /* Convert the strings into wide characters. */ - memset (&ps, '\0', sizeof (ps)); - p = pattern; -#ifdef _LIBC - n = __strnlen (pattern, 1024); -#else - n = strlen (pattern); -#endif - if (__glibc_likely (n < 1024)) - { - wpattern = (wchar_t *) alloca_account ((n + 1) * sizeof (wchar_t), - alloca_used); - n = mbsrtowcs (wpattern, &p, n + 1, &ps); - if (__glibc_unlikely (n == (size_t) -1)) - /* Something wrong. - XXX Do we have to set `errno' to something which mbsrtows hasn't - already done? */ - return -1; - if (p) - { - memset (&ps, '\0', sizeof (ps)); - goto prepare_wpattern; - } - } - else - { - prepare_wpattern: - n = mbsrtowcs (NULL, &pattern, 0, &ps); - if (__glibc_unlikely (n == (size_t) -1)) - /* Something wrong. - XXX Do we have to set `errno' to something which mbsrtows hasn't - already done? */ - return -1; - if (__glibc_unlikely (n >= (size_t) -1 / sizeof (wchar_t))) - { - __set_errno (ENOMEM); - return -2; - } - wpattern_malloc = wpattern - = (wchar_t *) malloc ((n + 1) * sizeof (wchar_t)); - assert (mbsinit (&ps)); - if (wpattern == NULL) - return -2; - (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps); - } + /* Calculate the size needed to convert the strings to wide + characters. */ + size_t patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1; + size_t strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1; + size_t totsize = patsize + strsize; - assert (mbsinit (&ps)); -#ifdef _LIBC - n = __strnlen (string, 1024); -#else - n = strlen (string); -#endif - p = string; - if (__glibc_likely (n < 1024)) - { - wstring = (wchar_t *) alloca_account ((n + 1) * sizeof (wchar_t), - alloca_used); - n = mbsrtowcs (wstring, &p, n + 1, &ps); - if (__glibc_unlikely (n == (size_t) -1)) - { - /* Something wrong. - XXX Do we have to set `errno' to something which - mbsrtows hasn't already done? */ - free_return: - free (wpattern_malloc); - return -1; - } - if (p) - { - memset (&ps, '\0', sizeof (ps)); - goto prepare_wstring; - } - } - else + struct scratch_buffer s; + scratch_buffer_init (&s); + if (!scratch_buffer_set_array_size (&s, totsize, sizeof (wchar_t))) { - prepare_wstring: - n = mbsrtowcs (NULL, &string, 0, &ps); - if (__glibc_unlikely (n == (size_t) -1)) - /* Something wrong. - XXX Do we have to set `errno' to something which mbsrtows hasn't - already done? */ - goto free_return; - if (__glibc_unlikely (n >= (size_t) -1 / sizeof (wchar_t))) - { - free (wpattern_malloc); - __set_errno (ENOMEM); - return -2; - } - - wstring_malloc = wstring - = (wchar_t *) malloc ((n + 1) * sizeof (wchar_t)); - if (wstring == NULL) - { - free (wpattern_malloc); - return -2; - } - assert (mbsinit (&ps)); - (void) mbsrtowcs (wstring, &string, n + 1, &ps); + scratch_buffer_free (&s); + errno = ENOMEM; + return -1; } - int res = internal_fnwmatch (wpattern, wstring, wstring + n, - flags & FNM_PERIOD, flags, NULL, - alloca_used); + wchar_t *wpattern = s.data; + wchar_t *wstring = wpattern + patsize; + + /* Convert the strings into wide characters. */ + mbsrtowcs (wpattern, &pattern, patsize, &ps); + mbsrtowcs (wstring, &string, strsize, &ps); + + int res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1, + flags & FNM_PERIOD, flags, NULL, 0); - free (wstring_malloc); - free (wpattern_malloc); + scratch_buffer_free (&s); return res; }