50f89d
Short description: fnmatch() fails with MBCS.
50f89d
Author(s): Fedora glibc team <glibc@lists.fedoraproject.org>
50f89d
Origin: PATCH
50f89d
Bug-RHEL: #819430, #826149, #826151
50f89d
Bug-Upstream: #14185
50f89d
Upstream status: not-submitted
50f89d
50f89d
fnmatch() fails when '*' wildcard is applied on the file name
50f89d
containing multi-byte character(s)
50f89d
50f89d
This needs to be reviewed thoroughly and go upstream with a
50f89d
new test case.
50f89d
50f89d
diff -Nrup a/posix/fnmatch.c b/posix/fnmatch.c
50f89d
--- a/posix/fnmatch.c	2012-01-01 07:16:32.000000000 -0500
50f89d
+++ b/posix/fnmatch.c	2012-05-23 14:14:29.099461189 -0400
50f89d
@@ -333,6 +333,7 @@ fnmatch (pattern, string, flags)
50f89d
 # if HANDLE_MULTIBYTE
50f89d
   if (__builtin_expect (MB_CUR_MAX, 1) != 1)
50f89d
     {
50f89d
+      const char *orig_pattern = pattern;
50f89d
       mbstate_t ps;
50f89d
       size_t n;
50f89d
       const char *p;
50f89d
@@ -356,10 +357,8 @@ fnmatch (pattern, string, flags)
50f89d
 						 alloca_used);
50f89d
 	  n = mbsrtowcs (wpattern, &p, n + 1, &ps);
50f89d
 	  if (__glibc_unlikely (n == (size_t) -1))
50f89d
-	    /* Something wrong.
50f89d
-	       XXX Do we have to set `errno' to something which mbsrtows hasn't
50f89d
-	       already done?  */
50f89d
-	    return -1;
50f89d
+	    /* Something wrong: Fall back to single byte matching. */
50f89d
+	    goto try_singlebyte;
50f89d
 	  if (p)
50f89d
 	    {
50f89d
 	      memset (&ps, '\0', sizeof (ps));
50f89d
@@ -371,10 +370,8 @@ fnmatch (pattern, string, flags)
50f89d
 	prepare_wpattern:
50f89d
 	  n = mbsrtowcs (NULL, &pattern, 0, &ps);
50f89d
 	  if (__glibc_unlikely (n == (size_t) -1))
50f89d
-	    /* Something wrong.
50f89d
-	       XXX Do we have to set `errno' to something which mbsrtows hasn't
50f89d
-	       already done?  */
50f89d
-	    return -1;
50f89d
+	    /*Something wrong: Fall back to single byte matching. */
50f89d
+	    goto try_singlebyte;
50f89d
 	  if (__glibc_unlikely (n >= (size_t) -1 / sizeof (wchar_t)))
50f89d
 	    {
50f89d
 	      __set_errno (ENOMEM);
50f89d
@@ -401,14 +398,8 @@ fnmatch (pattern, string, flags)
50f89d
 						alloca_used);
50f89d
 	  n = mbsrtowcs (wstring, &p, n + 1, &ps);
50f89d
 	  if (__glibc_unlikely (n == (size_t) -1))
50f89d
-	    {
50f89d
-	      /* Something wrong.
50f89d
-		 XXX Do we have to set `errno' to something which
50f89d
-		 mbsrtows hasn't already done?  */
50f89d
-	    free_return:
50f89d
-	      free (wpattern_malloc);
50f89d
-	      return -1;
50f89d
-	    }
50f89d
+	    /* Something wrong: Fall back to single byte matching. */
50f89d
+	    goto free_and_try_singlebyte;
50f89d
 	  if (p)
50f89d
 	    {
50f89d
 	      memset (&ps, '\0', sizeof (ps));
50f89d
@@ -420,10 +411,8 @@ fnmatch (pattern, string, flags)
50f89d
 	prepare_wstring:
50f89d
 	  n = mbsrtowcs (NULL, &string, 0, &ps);
50f89d
 	  if (__glibc_unlikely (n == (size_t) -1))
50f89d
-	    /* Something wrong.
50f89d
-	       XXX Do we have to set `errno' to something which mbsrtows hasn't
50f89d
-	       already done?  */
50f89d
-	    goto free_return;
50f89d
+	    /* Something wrong: Fall back to singlebyte matching. */
50f89d
+	    goto free_and_try_singlebyte;
50f89d
 	  if (__glibc_unlikely (n >= (size_t) -1 / sizeof (wchar_t)))
50f89d
 	    {
50f89d
 	      free (wpattern_malloc);
50f89d
@@ -450,6 +439,10 @@ fnmatch (pattern, string, flags)
50f89d
       free (wpattern_malloc);
50f89d
 
50f89d
       return res;
50f89d
+      free_and_try_singlebyte:
50f89d
+	free(wpattern_malloc);
50f89d
+      try_singlebyte:
50f89d
+	pattern = orig_pattern;
50f89d
     }
50f89d
 # endif  /* mbstate_t and mbsrtowcs or _LIBC.  */
50f89d