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