16,548
社区成员




#include "stdafx.h"
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char str[5] = {0};
strcpy_s(str, 5, "abcdef");
return 0;
}
// crt_truncate.c
#include <stdlib.h>
#include <errno.h>
int main()
{
char src[] = "1234567890";
char dst[5];
errno_t err = strncpy_s(dst, _countof(dst), src, _TRUNCATE);
if ( err == STRUNCATE )
printf( "truncation occurred!\n" );
printf( "'%s'\n", dst );
}
errno_t __cdecl memcpy_s(
void * dst,
size_t sizeInBytes,
const void * src,
size_t count
)
{
if (count == 0)
{
/* nothing to do */
return 0;
}
/* validation section */
_VALIDATE_RETURN_ERRCODE(dst != NULL, EINVAL);
if (src == NULL || sizeInBytes < count)
{
/* zeroes the destination buffer */
memset(dst, 0, sizeInBytes);
_VALIDATE_RETURN_ERRCODE(src != NULL, EINVAL);
_VALIDATE_RETURN_ERRCODE(sizeInBytes >= count, ERANGE);
/* useless, but prefast is confused */
return EINVAL;
}
memcpy(dst, src, count);
return 0;
}