From 08b2e62907111f9f33f1685952f0ffeb8edacf44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9=20van=20Geldorp?= Date: Mon, 2 Feb 2004 15:50:16 +0000 Subject: [PATCH] lstrcpyn is documented to always return a NUL terminated string svn path=/trunk/; revision=7994 --- reactos/lib/kernel32/string/lstring.c | 30 ++++++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/reactos/lib/kernel32/string/lstring.c b/reactos/lib/kernel32/string/lstring.c index 5ec20d04842..c72c1dc9298 100644 --- a/reactos/lib/kernel32/string/lstring.c +++ b/reactos/lib/kernel32/string/lstring.c @@ -51,21 +51,28 @@ lstrcpynA( ) { /* Can't use strncpy, because strncpy will fill unused bytes in - lpString1 with NUL bytes while lstrcpynA doesn't */ + lpString1 with NUL bytes while lstrcpynA doesn't. Also lstrcpynA + guarantees NUL termination while strncpy doesn't */ - if (0 != iMaxLength) + if (1 < iMaxLength) { char *d = lpString1; const char *s = lpString2; do { - if (0 == (*d++ = *s++)) + if ('\0' == (*d++ = *s++)) { break; } } - while(0 != --iMaxLength); + while(1 != --iMaxLength); + *d = '\0'; + } + else if (1 == iMaxLength) + { + /* Only space for the terminator */ + *lpString1 = '\0'; } return lpString1; @@ -153,21 +160,28 @@ lstrcpynW( ) { /* Can't use wcsncpy, because wcsncpy will fill unused bytes in - lpString1 with NUL bytes while lstrcpynW doesn't */ + lpString1 with NUL bytes while lstrcpynW doesn't Also lstrcpynW + guarantees NUL termination while wcsncpy doesn't */ - if (0 != iMaxLength) + if (1 < iMaxLength) { WCHAR *d = lpString1; const WCHAR *s = lpString2; do { - if (0 == (*d++ = *s++)) + if (L'\0' == (*d++ = *s++)) { break; } } - while(0 != --iMaxLength); + while(1 != --iMaxLength); + *d = L'\0'; + } + else if (1 == iMaxLength) + { + /* Only space for the terminator */ + *lpString1 = L'\0'; } return lpString1;