From patchwork Tue Aug 14 15:53:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 28897 Received: (qmail 88808 invoked by alias); 14 Aug 2018 15:53:15 -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 88794 invoked by uid 89); 14 Aug 2018 15:53:14 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-23.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Subject: Re: [PATCH] fnmatch: Use __mbstowcs_alloc [BZ #23519] To: libc-alpha@sourceware.org References: <20180814154642.09826405971F7@oldenburg.str.redhat.com> From: Florian Weimer Message-ID: <42836912-fedd-3a3a-97b9-6db10ca39519@redhat.com> Date: Tue, 14 Aug 2018 17:53:09 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <20180814154642.09826405971F7@oldenburg.str.redhat.com> Sorry, I posted the wrong version of the patch. Try this one. Subject: [PATCH] fnmatch: Use __mbstowcs_alloc [BZ #23519] To: libc-alpha@sourceware.org 2018-08-14 Florian Weimer [BZ #23519] * posix/fnmatch.c [HANDLE_MULTIBYTE] (fnmatch): Use __mbstowcs_alloc. diff --git a/posix/fnmatch.c b/posix/fnmatch.c index a9b762624f..1ce6c88d6a 100644 --- a/posix/fnmatch.c +++ b/posix/fnmatch.c @@ -35,6 +35,7 @@ #endif #ifdef _LIBC +# include # include #else # define alloca_account(size., var) alloca (size) @@ -322,122 +323,28 @@ fnmatch (const char *pattern, const char *string, int flags) # if HANDLE_MULTIBYTE if (__builtin_expect (MB_CUR_MAX, 1) != 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; + void *to_free1; + wchar_t wpattern_scratch[256]; + wchar_t *wpattern = __mbstowcs_alloc + (pattern, wpattern_scratch, array_length (wpattern_scratch), &to_free1); + if (wpattern == NULL) + return -1; - /* 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)) + void *to_free2; + wchar_t wstring_scratch[256]; + wchar_t *wstring = __mbstowcs_alloc + (string, wstring_scratch, array_length (wstring_scratch), &to_free2); + if (wstring == NULL) { - 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; - } + free (to_free1); + return -1; } - 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)); -#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 - { - 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); - } - - int res = internal_fnwmatch (wpattern, wstring, wstring + n, - flags & FNM_PERIOD, flags, NULL, - alloca_used); - - free (wstring_malloc); - free (wpattern_malloc); + int res = internal_fnwmatch (wpattern, + wstring, wstring + __wcslen (wstring), + flags & FNM_PERIOD, flags, NULL, 0); + free (to_free2); + free (to_free1); return res; } # endif /* mbstate_t and mbsrtowcs or _LIBC. */