updating cstr_helper to use secure string functions

This commit is contained in:
Michael Tryby
2019-04-02 13:48:38 -04:00
parent a89f3c9005
commit 3828ebb1cd

View File

@@ -9,16 +9,19 @@ int copy_cstr(const char *source, char **dest)
// Determines length, allocates memory, and returns a null terminated copy // Determines length, allocates memory, and returns a null terminated copy
// Be Aware: caller is responsible for freeing memory // Be Aware: caller is responsible for freeing memory
{ {
size_t len; size_t size;
len = strlen(source); size = 1 + strlen(source);
*dest = (char *) calloc((len + 1), sizeof(char)); *dest = (char *) calloc(size, sizeof(char));
if (*dest == NULL) if (*dest == NULL)
return -1; return -1;
else { else {
strncpy(*dest, source, (len + 1)); #ifdef _MSC_VER
strncat(*dest, "\0", 1); strncpy_s(*dest, size, source, size);
#else
strncpy(*dest, source, size);
#endif
} }
return 0; return 0;
} }
@@ -30,5 +33,4 @@ bool isnullterm_cstr(const char *source)
return true; return true;
else else
return false; return false;
} }