Skip to content

Instantly share code, notes, and snippets.

@styxyang
Created September 21, 2021 16:32
Show Gist options
  • Save styxyang/4b9699583545c3cc6f351bae612a8656 to your computer and use it in GitHub Desktop.
Save styxyang/4b9699583545c3cc6f351bae612a8656 to your computer and use it in GitHub Desktop.
secure string copy test
#include <stdio.h>
#include <string.h>
#include <securec.h>
#define HELLOWORLD "helloworld"
#define HELLOWORLD1 "helloworld1"
int main()
{
char name[sizeof(HELLOWORLD)] = HELLOWORLD;
char name1[sizeof(HELLOWORLD1)] = HELLOWORLD1;
char dest[sizeof(HELLOWORLD)] = { 0 };
int ret;
printf(" len %zu\n", strlen(name));
printf("size %zu\n", sizeof(name));
printf(" len %zu\n", strlen(name1));
printf("size %zu\n", sizeof(name1));
memset(dest, (char)0xff, sizeof(dest));
#if 1
ret = strncpy_s(dest, sizeof(dest), name1, sizeof(dest) - 1);
printf("ret %d\n", ret);
#else
strncpy(dest, name1, sizeof(dest) - 1);
#endif
printf(" len %zu\n", strlen(dest));
printf("size %zu\n", sizeof(dest));
printf("dest %s\n", dest);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment