From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-3.9 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 739A31F4B4 for ; Mon, 4 Jan 2021 20:25:47 +0000 (UTC) Received: from localhost ([::1]:52304 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kwWQE-0004D3-Ib for normalperson@yhbt.net; Mon, 04 Jan 2021 15:25:46 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49530) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kwWQ6-00044E-Lc for bug-gnulib@gnu.org; Mon, 04 Jan 2021 15:25:38 -0500 Received: from mail-qt1-x836.google.com ([2607:f8b0:4864:20::836]:37447) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kwWQ4-00066j-3O for bug-gnulib@gnu.org; Mon, 04 Jan 2021 15:25:37 -0500 Received: by mail-qt1-x836.google.com with SMTP id z9so19415632qtn.4 for ; Mon, 04 Jan 2021 12:25:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3H+RWXDKZAM2PpS/flEwWQuynf/UrJKZsy9zqIGb/ik=; b=R30zxO/nbn1wonWip4EiAWIbDEE7brupc+AjNlqdVp2A1zs+xPz9kdghCQAR9R1tcE 4bCe1RNMgRs1/gCbFCdPpsQwr+okZL457tYsHdAd803Qpo2QAtROGYM/Qk053ULr5qQD jv+EDpBlAClVdcKJHzf5FOuzTPFHkdjb1s2vCwgPSQxInpMiNFHazCgwNW/Xgb0v3eie N9xav8fH7XAPxIaxuzmPDB18LDuDl7bCDh7AWYaDC7K2sctW1uzSDa+NLO2e+AjHg4HT VgtN36mrRQAlsl5v4mBtVq9+tl1ffhufzQiTAmDXBdbQLfCy6exLRGIYDGT3L1mc54WN ksbQ== 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:mime-version :content-transfer-encoding; bh=3H+RWXDKZAM2PpS/flEwWQuynf/UrJKZsy9zqIGb/ik=; b=uID0tUci4tsgM/VA+xCsGCHFR8D697wHg9qHjMUUnxUAL2EN0asL6IlctP2g4tjVuG 6K63B9a48iPTHML25mluwx7I6bdZV/zJaFb6AcCuYZTmFyNZtBMm1Y+5WWRTLOYs4kAy 39viyVm3MXyY5epXPKN6H9bwtsQJjk4jF5dAHZwg91ucp23/HaVRV5WuG8KtQObSkS/D lGszdLrVp6HQeH1V4LyeUf7Pn85jC+5O5PXWbST9YcQysyaDqqG2AO4DBxJ6dSqrJGZ2 ZD5wP2w4v5KRG5nsg8vc9TAdmRnIZDs7MZjInrK/V2P+Fkke53lyk0/ythg+NkBEUaMa 7UEQ== X-Gm-Message-State: AOAM532BJPddDhAv8sONsqF2fo8em4R9UyEJ8tDyFRIJwBGM3rgUiJYr Yqk0sPNy6jw7KbAYdmzOXH8E7w== X-Google-Smtp-Source: ABdhPJxj/BZdyHHqGkNyUIGVs6j2rVfngvRydE2BElc8UQ9hU7lt3KKkVoLmugRUKYRadYbFQmiS/Q== X-Received: by 2002:ac8:6f07:: with SMTP id g7mr73365981qtv.308.1609791933655; Mon, 04 Jan 2021 12:25:33 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id b4sm36292928qtb.33.2021.01.04.12.25.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jan 2021 12:25:33 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH 1/2] posix: User scratch_buffer on fnmatch Date: Mon, 4 Jan 2021 17:25:27 -0300 Message-Id: <20210104202528.1228255-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::836; envelope-from=adhemerval.zanella@linaro.org; helo=mail-qt1-x836.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bug-gnulib@gnu.org Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" It removes the alloca usage on the string convertion to wide characters before calling the internal function. Checked on x86_64-linux-gnu. --- posix/fnmatch.c | 152 +++++++++++++++++------------------------------- 1 file changed, 53 insertions(+), 99 deletions(-) diff --git a/posix/fnmatch.c b/posix/fnmatch.c index 5896812c96..ac254fc9ac 100644 --- a/posix/fnmatch.c +++ b/posix/fnmatch.c @@ -75,6 +75,7 @@ extern int fnmatch (const char *pattern, const char *string, int flags); #include #include +#include #ifdef _LIBC typedef ptrdiff_t idx_t; @@ -231,119 +232,72 @@ is_char_class (const wchar_t *wcs) #include "fnmatch_loop.c" +static int +fnmatch_convert_to_wide (const char *str, struct scratch_buffer *buf, + size_t *n) +{ + mbstate_t ps; + memset (&ps, '\0', sizeof (ps)); + + size_t nw = buf->length / sizeof (wchar_t); + *n = strnlen (str, nw - 1); + if (__glibc_likely (*n < nw)) + { + const char *p = str; + *n = mbsrtowcs (buf->data, &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 == NULL) + return 0; + memset (&ps, '\0', sizeof (ps)); + } + + *n = mbsrtowcs (NULL, &str, 0, &ps); + if (__glibc_unlikely (*n == (size_t) -1)) + return -1; + if (!scratch_buffer_set_array_size (buf, *n + 1, sizeof (wchar_t))) + { + __set_errno (ENOMEM); + return -2; + } + assert (mbsinit (&ps)); + mbsrtowcs (buf->data, &str, *n + 1, &ps); + return 0; +} int fnmatch (const char *pattern, const char *string, int flags) { 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; + struct scratch_buffer wpattern; + scratch_buffer_init (&wpattern); + struct scratch_buffer wstring; + scratch_buffer_init (&wstring); + int r; /* Convert the strings into wide characters. */ - memset (&ps, '\0', sizeof (ps)); - p = pattern; - n = strnlen (pattern, 1024); - 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); - } - - assert (mbsinit (&ps)); - n = strnlen (string, 1024); - 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 + r = fnmatch_convert_to_wide (pattern, &wpattern, &n); + if (r != 0) + return r; + r = fnmatch_convert_to_wide (string, &wstring, &n); + if (r != 0) { - 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 (&wpattern); + return n; } - int res = internal_fnwmatch (wpattern, wstring, wstring + n, + int res = internal_fnwmatch (wpattern.data, wstring.data, + (wchar_t *) wstring.data + n, flags & FNM_PERIOD, flags, NULL, - alloca_used); + false); - free (wstring_malloc); - free (wpattern_malloc); + scratch_buffer_free (&wstring); + scratch_buffer_free (&wpattern); return res; } -- 2.25.1