* miscfuncs.cc (cygwin_wcsncasecmp): Never access more than n

characters.
	(cygwin_strncasecmp): Ditto.  Fix for strings longer than n.
This commit is contained in:
Corinna Vinschen 2008-02-01 13:11:57 +00:00
parent c69d873f31
commit 301d14d242
2 changed files with 27 additions and 17 deletions

View File

@ -1,3 +1,9 @@
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

View 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;
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);
}