Skip to content

Commit

Permalink
feat: copy strings and release after use
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschumann committed Aug 1, 2023
1 parent 39a6d80 commit 60c81b1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
42 changes: 25 additions & 17 deletions src/cunit.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ static unsigned int failure_count = 0;

static testsuite_t *create_testsuite(const char *name, setup_function_t setup,
teardown_function_t teardown) {
char *s;
testsuite_t *result;

result = malloc(sizeof(testsuite_t));
if (result == NULL) {
return NULL;
}

/* TODO copy name and free later */
result->name = name;
s = (char *)calloc(strlen(name) + 1, sizeof(char));
result->name = strcpy(s, name);
result->first_testcase = NULL;
result->setup = setup;
result->teardown = teardown;
Expand Down Expand Up @@ -60,15 +61,16 @@ testsuite_t *add_testsuite(const char *name, setup_function_t setup,
}

static testcase_t *create_testcase(const char *name, test_function_t function) {
char *s;
testcase_t *result;

result = malloc(sizeof(testcase_t));
if (result == NULL) {
return NULL;
}

/* TODO copy name and free later */
result->name = name;
s = (char *)calloc(strlen(name) + 1, sizeof(char));
result->name = strcpy(s, name);
result->function = function;
result->next = NULL;
return result;
Expand Down Expand Up @@ -161,33 +163,39 @@ static void print_summary(double run_time) {
}

static void clear_testcase(testcase_t *testcase) {
testcase_t *current_testcase, *next_testcase;
testcase_t *current, *next;

if (testcase == NULL) {
return;
}

current_testcase = testcase;
while (current_testcase != NULL) {
next_testcase = current_testcase->next;
free(current_testcase);
current_testcase = next_testcase;
current = testcase;
while (current != NULL) {
next = current->next;
if (current->name) {
free(current->name);
}
free(current);
current = next;
}
}

static void clear_testsuite(testsuite_t *testsuite) {
testsuite_t *current_testsuite, *next_testsuite;
testsuite_t *current, *next;

if (testsuite == NULL) {
return;
}

current_testsuite = testsuite;
while (current_testsuite != NULL) {
next_testsuite = current_testsuite->next;
clear_testcase(current_testsuite->first_testcase);
free(current_testsuite);
current_testsuite = next_testsuite;
current = testsuite;
while (current != NULL) {
next = current->next;
clear_testcase(current->first_testcase);
if (current->name) {
free(current->name);
}
free(current);
current = next;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/cunit.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ typedef void (*setup_function_t)(void);
typedef void (*teardown_function_t)(void);

typedef struct testcase_t {
const char *name;
char *name;
test_function_t function;
struct testcase_t *next;
} testcase_t;

typedef struct testsuite_t {
const char *name;
char *name;
struct testcase_t *first_testcase;
setup_function_t setup;
teardown_function_t teardown;
Expand Down

0 comments on commit 60c81b1

Please sign in to comment.