about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>2005-07-18 01:59:05 +0000
committerRoland McGrath <roland@gnu.org>2005-07-18 01:59:05 +0000
commit482bbae066d6bebfa342de2aff2353128bbd4453 (patch)
treeef83dfb6564661d8b46e552bd3844ef26dad8b43
parent9d5ab5c83eadda4eaaf581014cb84021818dc11d (diff)
downloadglibc-482bbae066d6bebfa342de2aff2353128bbd4453.tar.gz
glibc-482bbae066d6bebfa342de2aff2353128bbd4453.tar.xz
glibc-482bbae066d6bebfa342de2aff2353128bbd4453.zip
2005-03-29 Jakub Jelinek <jakub@redhat.com>
	[BZ #1087]
	* posix/fnmatch.c (fnmatch): For short patterns or strings attempt to
	avoid calling mbsrtowcs twice.
-rw-r--r--posix/fnmatch.c79
1 files changed, 61 insertions, 18 deletions
diff --git a/posix/fnmatch.c b/posix/fnmatch.c
index e409ed7300..3fa7c322d1 100644
--- a/posix/fnmatch.c
+++ b/posix/fnmatch.c
@@ -327,31 +327,74 @@ fnmatch (pattern, string, flags)
     {
       mbstate_t ps;
       size_t n;
+      const char *p;
       wchar_t *wpattern;
       wchar_t *wstring;
 
       /* Convert the strings into wide characters.  */
       memset (&ps, '\0', sizeof (ps));
-      n = mbsrtowcs (NULL, &pattern, 0, &ps);
-      if (__builtin_expect (n == (size_t) -1, 0))
-	/* Something wrong.
-	   XXX Do we have to set `errno' to something which mbsrtows hasn't
-	   already done?  */
-	return -1;
-      wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
-      assert (mbsinit (&ps));
-      (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
+      p = pattern;
+#ifdef _LIBC
+      n = strnlen (pattern, 1024);
+#else
+      n = strlen (pattern);
+#endif
+      if (__builtin_expect (n < 1024, 1))
+	{
+	  wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
+	  n = mbsrtowcs (wpattern, &p, n + 1, &ps);
+	  if (__builtin_expect (n == (size_t) -1, 0))
+	    /* 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));
+	}
+      if (__builtin_expect (p != NULL, 0))
+	{
+	  n = mbsrtowcs (NULL, &pattern, 0, &ps);
+	  if (__builtin_expect (n == (size_t) -1, 0))
+	    /* Something wrong.
+	       XXX Do we have to set `errno' to something which mbsrtows hasn't
+	       already done?  */
+	    return -1;
+	  wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
+	  assert (mbsinit (&ps));
+	  (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
+	}
 
       assert (mbsinit (&ps));
-      n = mbsrtowcs (NULL, &string, 0, &ps);
-      if (__builtin_expect (n == (size_t) -1, 0))
-	/* Something wrong.
-	   XXX Do we have to set `errno' to something which mbsrtows hasn't
-	   already done?  */
-	return -1;
-      wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
-      assert (mbsinit (&ps));
-      (void) mbsrtowcs (wstring, &string, n + 1, &ps);
+#ifdef _LIBC
+      n = strnlen (string, 1024);
+#else
+      n = strlen (string);
+#endif
+      p = string;
+      if (__builtin_expect (n < 1024, 1))
+	{
+	  wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
+	  n = mbsrtowcs (wstring, &p, n + 1, &ps);
+	  if (__builtin_expect (n == (size_t) -1, 0))
+	    /* 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));
+	}
+      if (__builtin_expect (p != NULL, 0))
+	{
+	  n = mbsrtowcs (NULL, &string, 0, &ps);
+	  if (__builtin_expect (n == (size_t) -1, 0))
+	    /* Something wrong.
+	       XXX Do we have to set `errno' to something which mbsrtows hasn't
+	       already done?  */
+	    return -1;
+	  wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
+	  assert (mbsinit (&ps));
+	  (void) mbsrtowcs (wstring, &string, n + 1, &ps);
+	}
 
       return internal_fnwmatch (wpattern, wstring, wstring + n,
 				flags & FNM_PERIOD, flags);