aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-01-23 16:54:09 +0100
committerdec05eba <dec05eba@protonmail.com>2020-01-23 16:54:09 +0100
commit26f8fbc2c657ecffc874410691dd3fc83ba11131 (patch)
treeafbabcf4e2af9e35eaf88bb0826b3fb9e3f3fe85
parent43ff02231117bacb3cd60af0e654a0b3e8b5678b (diff)
Fix hash map get_or_create crash
-rw-r--r--src/std/hash_map.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/std/hash_map.c b/src/std/hash_map.c
index 7ba1daf..9d8b4c4 100644
--- a/src/std/hash_map.c
+++ b/src/std/hash_map.c
@@ -44,8 +44,10 @@ void tsl_hash_map_deinit(TslHashMap *self) {
static int tsl_hash_map_append_bucket(TslHashMapNode **head_node, uint64_t hash, const TslStringView *key, size_t size, const void *data, void **output) {
TslHashMapNode *next_node;
uint8_t *node_data = malloc(sizeof(hash) + sizeof(key->size) + key->size + sizeof(size) + size);
+
if(output)
- *output = node_data;
+ *output = NULL;
+
if(!node_data) {
fprintf(stderr, "Error: hash map append failed. Reason: out of memory\n");
return 0;
@@ -54,8 +56,6 @@ static int tsl_hash_map_append_bucket(TslHashMapNode **head_node, uint64_t hash,
next_node = malloc(sizeof(TslHashMapNode));
if(!next_node) {
free(node_data);
- if(output)
- *output = NULL;
fprintf(stderr, "Error: hash map append failed. Reason: out of memory\n");
return 0;
}
@@ -72,6 +72,9 @@ static int tsl_hash_map_append_bucket(TslHashMapNode **head_node, uint64_t hash,
if(data)
memcpy(node_data + sizeof(hash) + sizeof(key->size) + key->size + sizeof(size), data, size);
+ if(output)
+ *output = node_data + sizeof(hash) + sizeof(key->size) + key->size + sizeof(size);
+
next_node->data = node_data;
if(*head_node) {
next_node->next = (*head_node)->next;