Skip to content

Commit

Permalink
dep_serialization: Fix deserialization some more
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwac committed Dec 7, 2024
1 parent 6c38f21 commit 0603523
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions src/dep_serialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,49 +100,39 @@ int dep_node_deserialize(dep_node_t* root, char* serialized) {
depth++;
}

// If the depth is one level higher than the previous one, we are a child dependency to the previous one.
// Then, we add the new dependency to our stack.

if (depth == prev_depth + 1) {
dep_node_t* const cur = stack[stack_size - 1];

// Actually create the node object.
// A depth which is more than one level higher than the previous one is impossible.

cur->children = realloc(cur->children, (cur->child_count + 1) * sizeof *cur->children);
assert(cur->children != NULL);
dep_node_t* const node = &cur->children[cur->child_count++];
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.
}

node->path = strdup(tok);
assert(node->path != NULL);
// A less than or equal to depth means that we've rolled back.
// Pop the extra stuff from the end of our stack.

node->child_count = 0;
node->children = NULL;
else if (depth <= prev_depth) {
stack_size = depth;
}

// Add a reference to this node to our stack.
dep_node_t* const cur = stack[stack_size - 1];

if (stack_size == 1) {
stack = realloc(stack, (stack_size + 1) * sizeof *stack);
assert(stack != NULL);
stack[stack_size++] = node;
}
// Actually create the node object.

continue;
}
cur->children = realloc(cur->children, (cur->child_count + 1) * sizeof *cur->children);
assert(cur->children != NULL);
dep_node_t* const node = &cur->children[cur->child_count++];

// A depth which is more than one level higher than the previous one is impossible.
node->path = strdup(tok + depth - 1);
assert(node->path != NULL);

else 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.
}
node->child_count = 0;
node->children = NULL;

// Any other depth means that we've rolled back.
// Pop the extra stuff from the end of our stack.
// Add a reference to this node to our stack.

else {
stack_size = depth + 1;
continue;
}
stack = realloc(stack, (stack_size + 1) * sizeof *stack);
assert(stack != NULL);
stack[stack_size++] = node;

prev_depth = depth;
}
Expand Down

0 comments on commit 0603523

Please sign in to comment.