Carlos O'Donell 0e17ea2
Short description: fnmatch() fails with MBCS.
Carlos O'Donell 0e17ea2
Author(s): Fedora glibc team <glibc@lists.fedoraproject.org>
Carlos O'Donell 0e17ea2
Origin: PATCH
Carlos O'Donell 0e17ea2
Bug-RHEL: #819430, #826149, #826151
Carlos O'Donell 0e17ea2
Bug-Upstream: #14185
Carlos O'Donell 0e17ea2
Upstream status: not-submitted
Carlos O'Donell 0e17ea2
Carlos O'Donell 0e17ea2
fnmatch() fails when '*' wildcard is applied on the file name
Carlos O'Donell 0e17ea2
containing multi-byte character(s)
Carlos O'Donell 0e17ea2
Carlos O'Donell 0e17ea2
This needs to be reviewed thoroughly and go upstream with a
Carlos O'Donell 0e17ea2
new test case.
Carlos O'Donell 0e17ea2
dfda51e
dfda51e
diff --git a/posix/fnmatch.c b/posix/fnmatch.c
dfda51e
index 5896812c966ac7c6..63df3dae0911030f 100644
dfda51e
--- a/posix/fnmatch.c
dfda51e
+++ b/posix/fnmatch.c
dfda51e
@@ -237,6 +237,7 @@ fnmatch (const char *pattern, const char *string, int flags)
dfda51e
 {
dfda51e
   if (__glibc_unlikely (MB_CUR_MAX != 1))
200aebf
     {
200aebf
+      const char *orig_pattern = pattern;
200aebf
       mbstate_t ps;
200aebf
       size_t n;
200aebf
       const char *p;
dfda51e
@@ -256,10 +257,8 @@ fnmatch (const char *pattern, const char *string, int flags)
dfda51e
                                                  alloca_used);
dfda51e
           n = mbsrtowcs (wpattern, &p, n + 1, &ps);
dfda51e
           if (__glibc_unlikely (n == (size_t) -1))
dfda51e
-            /* Something wrong.
dfda51e
-               XXX Do we have to set 'errno' to something which mbsrtows hasn't
dfda51e
-               already done?  */
dfda51e
-            return -1;
dfda51e
+	    /* Something wrong: Fall back to single byte matching.  */
200aebf
+	    goto try_singlebyte;
dfda51e
           if (p)
dfda51e
             {
dfda51e
               memset (&ps, '\0', sizeof (ps));
dfda51e
@@ -271,10 +270,8 @@ fnmatch (const char *pattern, const char *string, int flags)
dfda51e
         prepare_wpattern:
dfda51e
           n = mbsrtowcs (NULL, &pattern, 0, &ps);
dfda51e
           if (__glibc_unlikely (n == (size_t) -1))
dfda51e
-            /* Something wrong.
dfda51e
-               XXX Do we have to set 'errno' to something which mbsrtows hasn't
dfda51e
-               already done?  */
dfda51e
-            return -1;
dfda51e
+	    /* Something wrong: Fall back to single byte matching.  */
200aebf
+	    goto try_singlebyte;
dfda51e
           if (__glibc_unlikely (n >= (size_t) -1 / sizeof (wchar_t)))
dfda51e
             {
dfda51e
               __set_errno (ENOMEM);
dfda51e
@@ -297,14 +294,8 @@ fnmatch (const char *pattern, const char *string, int flags)
dfda51e
                                                 alloca_used);
dfda51e
           n = mbsrtowcs (wstring, &p, n + 1, &ps);
dfda51e
           if (__glibc_unlikely (n == (size_t) -1))
dfda51e
-            {
dfda51e
-              /* Something wrong.
dfda51e
-                 XXX Do we have to set 'errno' to something which
dfda51e
-                 mbsrtows hasn't already done?  */
dfda51e
-            free_return:
dfda51e
-              free (wpattern_malloc);
dfda51e
-              return -1;
dfda51e
-            }
dfda51e
+	    /* Something wrong: Fall back to single byte matching.  */
200aebf
+	    goto free_and_try_singlebyte;
dfda51e
           if (p)
dfda51e
             {
dfda51e
               memset (&ps, '\0', sizeof (ps));
dfda51e
@@ -316,10 +307,8 @@ fnmatch (const char *pattern, const char *string, int flags)
dfda51e
         prepare_wstring:
dfda51e
           n = mbsrtowcs (NULL, &string, 0, &ps);
dfda51e
           if (__glibc_unlikely (n == (size_t) -1))
dfda51e
-            /* Something wrong.
dfda51e
-               XXX Do we have to set 'errno' to something which mbsrtows hasn't
dfda51e
-               already done?  */
dfda51e
-            goto free_return;
200aebf
+	    /* Something wrong: Fall back to singlebyte matching. */
200aebf
+	    goto free_and_try_singlebyte;
dfda51e
           if (__glibc_unlikely (n >= (size_t) -1 / sizeof (wchar_t)))
dfda51e
             {
dfda51e
               free (wpattern_malloc);
dfda51e
@@ -346,6 +335,10 @@ fnmatch (const char *pattern, const char *string, int flags)
200aebf
       free (wpattern_malloc);
200aebf
 
200aebf
       return res;
200aebf
+      free_and_try_singlebyte:
200aebf
+	free(wpattern_malloc);
200aebf
+      try_singlebyte:
200aebf
+	pattern = orig_pattern;
200aebf
     }
200aebf
 
dfda51e
   return internal_fnmatch (pattern, string, string + strlen (string),