From 78aef8a3d7c762a56bdb3fd9e97114299f39df6b Mon Sep 17 00:00:00 2001 From: Aymeric Wibo Date: Wed, 11 Dec 2024 18:26:40 +0100 Subject: [PATCH] deps: Free stack and tree if error in deserialization --- src/dep_serialization.c | 12 ++++++++---- src/deps.c | 7 ++++--- src/deps.h | 1 + 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/dep_serialization.c b/src/dep_serialization.c index 0f152b8b..ea3c9539 100644 --- a/src/dep_serialization.c +++ b/src/dep_serialization.c @@ -60,6 +60,10 @@ char* dep_node_serialize(dep_node_t* node) { return add_children(NULL, node, 0); } +static void free_stack(dep_node_t*** stack) { + free(*stack); +} + int dep_node_deserialize(dep_node_t* root, char* serialized) { // Do all the necessary set up for the root node and the stack. @@ -70,7 +74,7 @@ int dep_node_deserialize(dep_node_t* root, char* serialized) { root->children = NULL; size_t stack_size = 1; - dep_node_t** stack = malloc(stack_size * sizeof *stack); + dep_node_t** __attribute__((cleanup(free_stack))) stack = malloc(stack_size * sizeof *stack); assert(stack != NULL); stack[0] = root; @@ -129,7 +133,9 @@ int dep_node_deserialize(dep_node_t* root, char* serialized) { if (depth > prev_depth + 1) { LOG_FATAL("Invalid depth in serialized dependency tree." PLZ_REPORT); - return -1; // TODO Error label to free the deserialized dependency tree up until now. Also the stack. + + deps_node_free(root); + return -1; } // A less than or equal to depth means that we've rolled back. @@ -162,7 +168,5 @@ int dep_node_deserialize(dep_node_t* root, char* serialized) { prev_depth = depth; } - free(stack); - return 0; } diff --git a/src/deps.c b/src/deps.c index dd0cb089..4af808c3 100644 --- a/src/deps.c +++ b/src/deps.c @@ -242,16 +242,17 @@ static int download(flamingo_val_t* deps_vec, dep_t* deps, uint64_t* hash) { return 0; } -static void free_node(dep_node_t* node) { +void deps_node_free(dep_node_t* node) { for (size_t i = 0; i < node->child_count; i++) { - free_node(&node->children[i]); + deps_node_free(&node->children[i]); } free(node->children); + free(node->path); } void deps_tree_free(dep_node_t* tree) { - free_node(tree); + deps_node_free(tree); free(tree); } diff --git a/src/deps.h b/src/deps.h index 97825acf..6674f8ae 100644 --- a/src/deps.h +++ b/src/deps.h @@ -23,6 +23,7 @@ struct dep_node_t { }; dep_node_t* deps_tree(flamingo_val_t* deps_vec, size_t path_len, uint64_t* path_hashes, bool* circular); +void deps_node_free(dep_node_t* node); void deps_tree_free(dep_node_t* tree); // Dependency serialization.