Skip to content

Commit

Permalink
deps: Free stack and tree if error in deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwac committed Dec 11, 2024
1 parent bd05132 commit 78aef8a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/dep_serialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -162,7 +168,5 @@ int dep_node_deserialize(dep_node_t* root, char* serialized) {
prev_depth = depth;
}

free(stack);

return 0;
}
7 changes: 4 additions & 3 deletions src/deps.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions src/deps.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 78aef8a

Please sign in to comment.