From 88c6922a9027005af5db9000d1ef5e6c60da722e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9=20van=20Geldorp?= Date: Wed, 28 Jan 2004 08:51:09 +0000 Subject: [PATCH] strncpy does add NUL bytes to fill dest buffer completely, however lstrcpyn does not. svn path=/trunk/; revision=7893 --- reactos/lib/kernel32/string/lstring.c | 40 +++++++++++++++++++++++++-- reactos/lib/string/i386/tcsncpy.h | 3 +- reactos/lib/string/tcsncpy.h | 3 +- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/reactos/lib/kernel32/string/lstring.c b/reactos/lib/kernel32/string/lstring.c index dddc1506d9a..5ec20d04842 100644 --- a/reactos/lib/kernel32/string/lstring.c +++ b/reactos/lib/kernel32/string/lstring.c @@ -50,7 +50,25 @@ lstrcpynA( int iMaxLength ) { - return strncpy(lpString1,lpString2,iMaxLength); + /* Can't use strncpy, because strncpy will fill unused bytes in + lpString1 with NUL bytes while lstrcpynA doesn't */ + + if (0 != iMaxLength) + { + char *d = lpString1; + const char *s = lpString2; + + do + { + if (0 == (*d++ = *s++)) + { + break; + } + } + while(0 != --iMaxLength); + } + + return lpString1; } @@ -134,7 +152,25 @@ lstrcpynW( int iMaxLength ) { - return wcsncpy(lpString1,lpString2,iMaxLength); + /* Can't use wcsncpy, because wcsncpy will fill unused bytes in + lpString1 with NUL bytes while lstrcpynW doesn't */ + + if (0 != iMaxLength) + { + WCHAR *d = lpString1; + const WCHAR *s = lpString2; + + do + { + if (0 == (*d++ = *s++)) + { + break; + } + } + while(0 != --iMaxLength); + } + + return lpString1; } diff --git a/reactos/lib/string/i386/tcsncpy.h b/reactos/lib/string/i386/tcsncpy.h index 93a484a617c..8159b008654 100644 --- a/reactos/lib/string/i386/tcsncpy.h +++ b/reactos/lib/string/i386/tcsncpy.h @@ -1,4 +1,4 @@ -/* $Id: tcsncpy.h,v 1.2 2004/01/27 21:43:47 gvg Exp $ +/* $Id: tcsncpy.h,v 1.3 2004/01/28 08:51:09 gvg Exp $ */ #include "tchar.h" @@ -22,6 +22,7 @@ _tcsncpy: _tstos test %_treg(a), %_treg(a) jnz .L1 + rep _tstos .L2: mov 0x0C(%esp), %eax diff --git a/reactos/lib/string/tcsncpy.h b/reactos/lib/string/tcsncpy.h index 03b363bb54b..70e63dcc6b5 100644 --- a/reactos/lib/string/tcsncpy.h +++ b/reactos/lib/string/tcsncpy.h @@ -1,4 +1,4 @@ -/* $Id: tcsncpy.h,v 1.2 2004/01/27 21:43:47 gvg Exp $ +/* $Id: tcsncpy.h,v 1.3 2004/01/28 08:51:09 gvg Exp $ */ #include @@ -15,6 +15,7 @@ _TCHAR * _tcsncpy(_TCHAR * dst, const _TCHAR * src, size_t n) { if((*d ++ = *s ++) == 0) { + while (-- n != 0) *d ++ = 0; break; } }