From 19367a27472a06634424e56c1eb21e4f53da4e9e Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Sun, 8 Jan 2023 15:39:19 -0800 Subject: fixed invalid read; some memory leakage fixed but not completely --- src/hash_table.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/hash_table.c') diff --git a/src/hash_table.c b/src/hash_table.c index 29946b1..f8d97a2 100644 --- a/src/hash_table.c +++ b/src/hash_table.c @@ -24,6 +24,11 @@ sl_node_t *init_sl_node(char *key, ast_t *value) { return n; } +void sl_node_free(sl_node_t *n) { + ast_free(n->value->value); + free(n->value); + free(n); +} /*** SINGLY LINKED LIST FUNCTIONS ***/ sl_list_t *init_sl_list() { sl_list_t *l = (sl_list_t *)malloc(sizeof(sl_list_t)); @@ -108,14 +113,24 @@ bool sl_list_exists(sl_list_t *l, char *key) { void sl_list_free(sl_list_t *l) { sl_node_t *cur = l->head; sl_node_t *tmp; - for (int i = 0; i < l->size; i++) { + while (cur != NULL) { tmp = cur; - cur = cur->next; - free(tmp); + cur = tmp->next; + sl_node_free(tmp); } free(l); } +void sl_list_free_some(sl_list_t *l) { + sl_node_t *cur = l->head; + sl_node_t *tmp; + while (cur != NULL) { + tmp = cur; + cur = tmp->next; + free(tmp); + } + free(l); +} /*** HASH TABLE FUNCTIONS ***/ hash_table_t *init_hash_table(int size) { hash_table_t *h = (hash_table_t *)malloc(sizeof(hash_table_t)); @@ -153,6 +168,11 @@ void hash_table_free(hash_table_t *h) { free(h); } +void hash_table_free_some(hash_table_t *h) { + for (int i = 0; i < h->size; i++) + sl_list_free_some(h->buckets[i]); + free(h); +} /* DJB2 HASH FUNCTION */ unsigned long hash(char *key, int size) { unsigned long hash = 5381; -- cgit