Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defensive frees #41

Merged
merged 2 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bitarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const unsigned char* ba_get_bitarray(bitarray_t ba) {

void ba_free(bitarray_t ba) {
free(ba->arr);
ba->arr = NULL;
ba->num_bits = 0;
ba->num_chars = 0;
free(ba);
Expand Down
8 changes: 7 additions & 1 deletion src/dllist.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ void dll_free(dllist_t l) {
void dll_free_alt(dllist_t l, bool free_data) {
dll_node* n = l->head;
while (n != NULL) {
if (free_data == true)
if (free_data == true) {
free(n->data);
n->data = NULL;
}
dll_node* t = n;
n = n->next;
free(t);
t = NULL;
}
l->head = NULL;
l->tail = NULL;
l->elms = 0;
free(l);
}
Expand Down Expand Up @@ -156,5 +161,6 @@ void* dll_remove(dllist_t l, int idx) {
data = ret->data;
--(l->elms);
free(ret);
ret = NULL;
return data;
}
26 changes: 26 additions & 0 deletions src/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,19 @@ void f_free(file_t f) {
f->lines[i] = NULL;
free(f->lines);
free(f->buffer);

/* Set everything to a default value */
f->filesize = 0;
f->mode = 0;
f->num_lines = 0;
f->is_symlink = false;
f->basepath = NULL;
f->filename = NULL;
f->extension = NULL;
f->absolute_path = NULL;
f->buffer = NULL;
f->lines = NULL;

free(f);
}

Expand Down Expand Up @@ -571,6 +584,19 @@ void d_free(dir_t d) {
__free_double_array(d->subitems_fullpath, d->num_subitems);

free(d->full_path);

/* Set everything to a default value */
d->num_subitems = 0;
d->num_subdirs = 0;
d->num_subfiles = 0;
d->full_path = NULL;
d->subitems = NULL;
d->subdirs = NULL;
d->subfiles = NULL;
d->subitems_fullpath = NULL;
d->subdirs_fullpath = NULL;
d->subfiles_fullpath = NULL;

free(d);
}

Expand Down
6 changes: 6 additions & 0 deletions src/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ void g_free_alt(graph_t g, bool free_metadata) {
g->_max_edges = 0;
g->_prev_vert_id = 0;
g->_prev_edge_id = 0;
g->verts = NULL;
g->edges = NULL;

free(g);
}
Expand Down Expand Up @@ -320,6 +322,9 @@ void g_vertex_free_alt(vertex_t v, bool free_metadata) {
free(v->edges);
if (free_metadata == true)
free(v->metadata);

v->metadata = NULL;
v->edges = NULL;
free(v);
}

Expand Down Expand Up @@ -359,6 +364,7 @@ void g_edge_free_alt(edge_t e, bool free_metadata) {
e->dest = 0;
if (free_metadata == true)
free(e->metadata);
e->metadata = NULL;
free(e);
}

Expand Down
4 changes: 4 additions & 0 deletions src/llist.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ void ll_free_alt(llist_t l, bool free_data) {
while (n != NULL) {
if (free_data == true)
free(n->data);
n->data = NULL;
ll_node* t = n->next;
free(n);
n = t;
}

l->head = NULL;
l->elms = 0;
free(l);
}

Expand Down
8 changes: 7 additions & 1 deletion src/permutations.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
typedef struct __permutations {
unsigned short alphabet_len;
size_t input_len;
char* alphabet;
unsigned short* cur_perm;
char* cur_perm_str;
char* alphabet;
} __permutations;


Expand All @@ -41,6 +41,12 @@ void perm_free(permutations_t p) {
free(p->alphabet);
free(p->cur_perm);
free(p->cur_perm_str);

p->alphabet_len = 0;
p->input_len = 0;
p->cur_perm = NULL;
p->cur_perm_str = NULL;
p->alphabet = NULL;
free(p);
}

Expand Down
3 changes: 3 additions & 0 deletions src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ void q_free_alt(queue_list_t q, bool free_data) {
while (n != NULL) {
if (free_data == true)
free(n->data);
n->data = NULL;
queue_node* t = n;
n = n->next;
free(t);
}
q->elms = 0;
q->head = NULL;
q->tail = NULL;
free(q);
}

Expand Down
2 changes: 2 additions & 0 deletions src/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ void stk_free_alt(stack_list_t stk, bool free_data) {
while (n != NULL) {
if (free_data == true)
free(n->data);
n->data = NULL;
stack_node* t = n;
n = n->next;
free(t);
}
stk->elms = 0;
stk->head = NULL;
free(stk);
}

Expand Down
4 changes: 3 additions & 1 deletion src/stringlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

void s_free_array_of_strings(char** a, int num) {
int i;
for (i = 0; i < num; ++i)
for (i = 0; i < num; ++i) {
free(a[i]);
a[i] = NULL;
}
free(a);
}

Expand Down
3 changes: 1 addition & 2 deletions src/stringlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
***
*******************************************************************************/

#include <stdio.h>

#define CASE_SENSITIVE 1
#define CASE_INSENSITIVE 0

#define STRLIB_VERSION "0.1.0"
#define S_STRLIB_VERSION "0.1.1"


void s_free_array_of_strings(char** a, int num);
Expand Down