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

View File

@@ -3,7 +3,11 @@
#define CSTR_HELPER_H_
int copy_cstr(const char *source, char **destination, size_t *size);
#include <stdbool.h>
int copy_cstr(const char *source, char **destination);
bool isnullterm_cstr(const char *source);
#endif /* CSTR_HELPER_H_ */

View File

@@ -27,15 +27,14 @@
typedef struct file_s {
char *filename;
size_t size;
char *filename; // Assumes this is a null terminated string
FILE *file;
} file_handle_t;
// local (private) functions
int _fopen(FILE **f, const char *name, const char *mode);
int _get_temp_filename(char **tempname, size_t *size);
int _get_temp_filename(char **tempname);
file_handle_t *create_file_manager() {
@@ -44,7 +43,6 @@ file_handle_t *create_file_manager() {
file_handle = (file_handle_t *)calloc(1, sizeof(file_handle_t));
file_handle->filename = NULL;
file_handle->size = 0;
file_handle->file = NULL;
return file_handle;
@@ -65,7 +63,7 @@ void get_filename(file_handle_t *file_handle, char **filename, size_t *size)
// BE AWARE: The memory allocated here must be freed by the caller
//
{
copy_cstr(file_handle->filename, filename, size);
copy_cstr(file_handle->filename, filename);
}
@@ -73,9 +71,9 @@ int open_file(file_handle_t *file_handle, const char *filename, const char *file
int error = 0;
if (filename == NULL)
_get_temp_filename(&(file_handle->filename), &(file_handle->size));
_get_temp_filename(&(file_handle->filename));
else
copy_cstr(filename, &(file_handle->filename), &(file_handle->size));
copy_cstr(filename, &(file_handle->filename));
if (file_mode == NULL)
error = -1;
@@ -173,7 +171,7 @@ int _fopen(FILE **f, const char *name, const char *mode)
return ret;
}
int _get_temp_filename(char **tempname, size_t *size)
int _get_temp_filename(char **tempname)
{
int error = 0;
@@ -187,7 +185,7 @@ int _get_temp_filename(char **tempname, size_t *size)
return error;
}
else
copy_cstr(name, tempname, size);
copy_cstr(name, tempname);
// --- free the pointer returned by _tempnam
if (name)
@@ -201,3 +199,12 @@ int _get_temp_filename(char **tempname, size_t *size)
#endif
return error;
}
bool is_valid(file_handle_t *file_handle)
{
if ((file_handle->filename == NULL && file_handle->file == NULL) ||
(isnullterm_cstr(file_handle->filename) && file_handle != NULL))
return true;
else
return false;
}

View File

@@ -71,6 +71,9 @@ int close_file(file_handle_t *file_handle);
int remove_file(file_handle_t *file_handle);
bool is_valid(file_handle_t *file_handle);
#if defined(__cplusplus)
}
#endif

View File

@@ -40,6 +40,7 @@ BOOST_AUTO_TEST_CASE (test_create_destroy)
file_handle = create_file_manager();
BOOST_CHECK(file_handle != NULL);
BOOST_CHECK(is_valid(file_handle) == true);
delete_file_manager(file_handle);
}
@@ -54,6 +55,7 @@ BOOST_AUTO_TEST_CASE(test_open_close)
error = open_file(file_handle, DATA_PATH_OUTPUT, "rb");
BOOST_REQUIRE(error == 0);
BOOST_CHECK(is_valid(file_handle) == true);
error = close_file(file_handle);
BOOST_REQUIRE(error == 0);
@@ -85,7 +87,7 @@ BOOST_FIXTURE_TEST_CASE(test_temp_file, Fixture)
printf_file(file_handle, "%s", "This is a test.");
get_filename(file_handle, &filename, &size);
//BOOST_CHECK(check_string(filename, "./test_file.txt"));
BOOST_CHECK(is_valid(file_handle) == true);
BOOST_CHECK(boost::filesystem::exists(filename) == true);