OSDN Git Service

* miscfuncs.cc (cygwin_wcsncasecmp): Never access more than n
authorcorinna <corinna>
Fri, 1 Feb 2008 13:11:55 +0000 (13:11 +0000)
committercorinna <corinna>
Fri, 1 Feb 2008 13:11:55 +0000 (13:11 +0000)
characters.
(cygwin_strncasecmp): Ditto.  Fix for strings longer than n.

winsup/cygwin/ChangeLog
winsup/cygwin/miscfuncs.cc

index ea7b93f..3720bab 100644 (file)
@@ -1,5 +1,11 @@
 2008-02-01  Corinna Vinschen  <corinna@vinschen.de>
 
+       * miscfuncs.cc (cygwin_wcsncasecmp): Never access more than n
+       characters.
+       (cygwin_strncasecmp): Ditto.  Fix for strings longer than n.
+
+2008-02-01  Corinna Vinschen  <corinna@vinschen.de>
+
        * string.h: Re-enable inline strcasematch and strncasematch
        implementations and rename to ascii_strcasematch/ascii_strncasematch.
        * dcrt0.cc: Replace str[n]casematch with ascii_str[n]casematch where
index c6328cb..0ec0b48 100644 (file)
@@ -17,6 +17,8 @@ details. */
 #include <alloca.h>
 #include <limits.h>
 #include <wchar.h>
+#include <winbase.h>
+#include <winnls.h>
 #include "cygthread.h"
 #include "cygtls.h"
 #include "ntdll.h"
@@ -94,14 +96,14 @@ extern "C" int __stdcall
 cygwin_wcsncasecmp (const wchar_t  *ws, const wchar_t *wt, size_t n)
 {
   UNICODE_STRING us, ut;
-
-  n *= sizeof (WCHAR);
-  RtlInitUnicodeString (&us, ws);
-  if (us.Length > n)
-    us.Length = n;
-  RtlInitUnicodeString (&ut, wt);
-  if (ut.Length > n)
-    ut.Length = n;
+  size_t ls = 0, lt = 0;
+
+  while (ws[ls] && ls < n)
+    ++ls;
+  RtlInitCountedUnicodeString (&us, ws, ls * sizeof (WCHAR));
+  while (wt[lt] && lt < n)
+    ++lt;
+  RtlInitCountedUnicodeString (&ut, wt, lt * sizeof (WCHAR));
   return RtlCompareUnicodeString (&us, &ut, TRUE);
 }
 
@@ -125,18 +127,20 @@ cygwin_strncasecmp (const char *cs, const char *ct, size_t n)
 {
   UNICODE_STRING us, ut;
   ULONG len;
-  
-  n *= sizeof (WCHAR);
-  len = (strlen (cs) + 1) * sizeof (WCHAR);
+  size_t ls = 0, lt = 0;
+
+  while (cs[ls] && ls < n)
+    ++ls;
+  len = ls * sizeof (WCHAR);
   RtlInitEmptyUnicodeString (&us, (PWCHAR) alloca (len), len);
-  us.Length = sys_mbstowcs (us.Buffer, cs, us.MaximumLength) * sizeof (WCHAR);
-  if (us.Length > n)
-    us.Length = n;
-  len = (strlen (ct) + 1) * sizeof (WCHAR);
+  us.Length = MultiByteToWideChar (get_cp (), 0, cs, ls, us.Buffer,
+                                  us.MaximumLength)  * sizeof (WCHAR);
+  while (ct[lt] && lt < n)
+    ++lt;
+  len = lt * sizeof (WCHAR);
   RtlInitEmptyUnicodeString (&ut, (PWCHAR) alloca (len), len);
-  ut.Length = sys_mbstowcs (ut.Buffer, ct, ut.MaximumLength) * sizeof (WCHAR);
-  if (ut.Length > n)
-    ut.Length = n;
+  ut.Length = MultiByteToWideChar (get_cp (), 0, ct, lt, ut.Buffer,
+                                  ut.MaximumLength)  * sizeof (WCHAR);
   return RtlCompareUnicodeString (&us, &ut, TRUE);
 }