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: 869280 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-89974-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="VJXuKzxQ"; 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 3zZpN63flWz9t2l for ; Tue, 6 Feb 2018 00:29:26 +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=bbSOI5Di6PJR1Tb/vHViqlDWmIk5REu zH2tgd34Hvy5mz14FAV7dOpFNUwMaDfvW2vu6yeVULK/67ME1fTmY/a/jqZ0br1e Ndq5CD1VpdjXEBzB4gLeyG6D5crbMA/xMPmWwIcRPmgoKNK/Ks0u6unPjF9Jibgs K1prskNdO0QI= 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=VJXuK zxQBlOCn4vIstvmB2e5kT44Gl7VyFZ53OrjDzfsDuyVKZnXg3HtWOZLqYKsPL++G q4CPoUA1+csL0cxV4TSQANmxkHfCrAcLoRrMEVDlRePm74PpoFVu8/UBbAzdM6QU hoTKpGvkacJB7xJAlZ7Fo0cfXc6vDxaEN6HyjQ= 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-Google-Smtp-Source: AH8x227ls9/i+UupsD4jjNFjd7FuQd3ADSsl8cVPy/4f289DQIPcseyN98DoMsgSaSHJDQq06WFDlA== 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(-) 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; }