Making sure filename is null terminated string

Added file_handle validation
This commit is contained in:
Michael Tryby
2019-04-02 12:31:05 -04:00
parent c98d13de80
commit a89f3c9005
5 changed files with 45 additions and 17 deletions

View File

@@ -2,21 +2,33 @@
#include <stdlib.h>
#include <string.h>
#include "cstr_helper.h"
int copy_cstr(const char *source, char **destination, size_t *size)
int copy_cstr(const char *source, char **dest)
// Determines length, allocates memory, and returns a null terminated copy
// Be Aware: caller is responsible for freeing memory
{
size_t len;
len = strlen(source);
*destination = (char *) calloc(len + 1, sizeof(char));
*dest = (char *) calloc((len + 1), sizeof(char));
if (*destination == NULL)
if (*dest == NULL)
return -1;
else {
strncpy(*destination, source, len);
*size = len + 1;
strncpy(*dest, source, (len + 1));
strncat(*dest, "\0", 1);
}
return 0;
}
bool isnullterm_cstr(const char *source)
{
if (strchr(source, '\0'))
return true;
else
return false;
}