From 01eb5ece551c4ab10eea4d14bd0f55766b52b8fd Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 7 Dec 2022 13:29:21 +0100 Subject: [PATCH] fix: fixed segfault --- Makefile | 5 +++++ trie/src/trie.c | 15 ++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index edc6431..0d9d919 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,11 @@ prod: cmake-release run: build @ LANDER_DATA_DIR=data LANDER_BASE_URL=http://localhost:18080/ LANDER_API_KEY=test ./build/Debug/lander +.PHONY: valgrind +valgrind: build + @ LANDER_DATA_DIR=data LANDER_BASE_URL=http://localhost:18080/ LANDER_API_KEY=test \ + valgrind --tool=memcheck --error-exitcode=1 --track-origins=yes --leak-check=full ./build/Debug/lander + .PHONY: gdb gdb: build @ LANDER_DATA_DIR=data LANDER_BASE_URL=http://localhost:18080/ LANDER_API_KEY=test gdb --args ./build/Debug/lander diff --git a/trie/src/trie.c b/trie/src/trie.c index 176d5a0..9803b79 100644 --- a/trie/src/trie.c +++ b/trie/src/trie.c @@ -28,10 +28,9 @@ TrieExitCode trie_init(Trie **trie_ptr, const char *file_path) { trie->root = tnode_init(); pthread_rwlock_init(&trie->lock, NULL); - *trie_ptr = trie; - if (file_path == NULL) { trie->file_path = NULL; + *trie_ptr = trie; return Ok; } @@ -50,7 +49,7 @@ TrieExitCode trie_init(Trie **trie_ptr, const char *file_path) { Entry *entry; char *string; int i, j; - int entries = 0; + TrieExitCode status; while (fgets(buffer, 8192, fp)) { i = 0; @@ -76,13 +75,18 @@ TrieExitCode trie_init(Trie **trie_ptr, const char *file_path) { buffer[j] = '\0'; entry = entry_new(type, buffer + i + 3); - trie_add_no_lock(trie, buffer, entry); + status = trie_add_no_lock(trie, buffer, entry); - entries++; + if (status != Ok) { + trie_free(trie); + return status; + } } fclose(fp); + *trie_ptr = trie; + return Ok; } @@ -266,6 +270,7 @@ TrieExitCode trie_add_no_lock(Trie *trie, const char *string, Entry *entry) { } (*child_node_ptr)->represents = true; + (*child_node_ptr)->entry = entry; trie->size++; return Ok; }