Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strmap unset segfault when left child is rightmost node #501

Merged
merged 1 commit into from
Oct 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/common/strmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,17 +765,20 @@ int strmap_unset(strmap* tree, const char* key)

/* found it, identify the node to replace it */
if (node->left != NULL && node->right != NULL) {
/* we have two children, extract the rightmost node in
* our left subtree */
/* we have two children, identify rightmost node of left subtree */
strmap_node* replacement = (strmap_node*) strmap_node_rightmost(node->left);
strmap_node_extract_single(replacement);

/* update the left child of this node to point to our left child,
* (note that this works correctly even if the replacement is our
* original left child, because the extract call would update our
* left child to be our left grandchild) */
replacement->left = node->left;
node->left->parent = replacement;

/* if the rightmost node of our left subtree is not our left child,
* extract it and promote it to be our replacement */
if (replacement != node->left) {
/* since this is a rightmost node, it has at most one child,
* so safe to use extract_single */
strmap_node_extract_single(replacement);

/* update the left child of this node to point to our replacement */
replacement->left = node->left;
node->left->parent = replacement;
}

/* update the right child of this node to point to our right child,
* (we're guaranteed that the rightmost node from our left subtree
Expand Down