strncpy does add NUL bytes to fill dest buffer completely,

however lstrcpyn does not.

svn path=/trunk/; revision=7893
This commit is contained in:
Gé van Geldorp 2004-01-28 08:51:09 +00:00
parent 18a0b41cb6
commit 88c6922a90
3 changed files with 42 additions and 4 deletions

View file

@ -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;
}

View file

@ -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

View file

@ -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 <stddef.h>
@ -15,6 +15,7 @@ _TCHAR * _tcsncpy(_TCHAR * dst, const _TCHAR * src, size_t n)
{
if((*d ++ = *s ++) == 0)
{
while (-- n != 0) *d ++ = 0;
break;
}
}