From 301d14d242268597570d4b38a84c426e7f3baf5b Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Fri, 1 Feb 2008 13:11:57 +0000 Subject: [PATCH] * miscfuncs.cc (cygwin_wcsncasecmp): Never access more than n characters. (cygwin_strncasecmp): Ditto. Fix for strings longer than n. --- winsup/cygwin/ChangeLog | 6 ++++++ winsup/cygwin/miscfuncs.cc | 38 +++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index ea7b93f2a..3720bab56 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,9 @@ +2008-02-01 Corinna Vinschen + + * miscfuncs.cc (cygwin_wcsncasecmp): Never access more than n + characters. + (cygwin_strncasecmp): Ditto. Fix for strings longer than n. + 2008-02-01 Corinna Vinschen * string.h: Re-enable inline strcasematch and strncasematch diff --git a/winsup/cygwin/miscfuncs.cc b/winsup/cygwin/miscfuncs.cc index c6328cb18..0ec0b4873 100644 --- a/winsup/cygwin/miscfuncs.cc +++ b/winsup/cygwin/miscfuncs.cc @@ -17,6 +17,8 @@ details. */ #include #include #include +#include +#include #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; + size_t ls = 0, lt = 0; - n *= sizeof (WCHAR); - RtlInitUnicodeString (&us, ws); - if (us.Length > n) - us.Length = n; - RtlInitUnicodeString (&ut, wt); - if (ut.Length > n) - ut.Length = n; + 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); }