Skip to content

Commit

Permalink
lyds tree BUGFIX for rb_remove_node()
Browse files Browse the repository at this point in the history
Added based on static code analysis.
  • Loading branch information
lePici committed Feb 27, 2024
1 parent 8760d96 commit 09fd71d
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/tree_data_sorted.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,13 @@ rb_compare_lists(const struct lyd_node *n1, const struct lyd_node *n2)
/**
* @brief Release unlinked red-black node.
*
* @param[in] rbn Node to free.
* @param[in,out] rbn Node to free, is set to NULL.
*/
static void
rb_free_node(struct rb_node *rbn)
rb_free_node(struct rb_node **rbn)
{
free(rbn);
free(*rbn);
*rbn = NULL;
}

/**
Expand Down Expand Up @@ -356,7 +357,7 @@ lyds_free_tree(struct rb_node *rbt)

/* There is no rebalancing. */
for (rbn = rb_iter_begin(rbt, &iter_state); rbn; rbn = rb_iter_next(&iter_state)) {
rb_free_node(rbn);
rb_free_node(&rbn);
}
}

Expand Down Expand Up @@ -935,7 +936,7 @@ lyds_pool_clean(struct lyds_pool *pool)
struct rb_node *iter;

for (iter = pool->rbn; iter; iter = rb_iter_next(&pool->iter_state)) {
rb_free_node(iter);
rb_free_node(&iter);
}
pool->rbn = NULL;

Expand Down Expand Up @@ -977,6 +978,10 @@ rb_remove_node(struct lyd_meta *root_meta, struct rb_node **rbt, struct lyd_node
/* remove node */
rbn = rb_remove(rbt, rbn);
*removed = rbn;
if (rbn == *rbt) {
/* rbn was the last node, assurance that root will be set to NULL */
*rbt = NULL;
}

/* the root of the Red-black tree may changed due to removal, so update the pointer to the root */
RBT_SET(root_meta, *rbt);
Expand Down Expand Up @@ -1349,7 +1354,7 @@ lyds_unlink(struct lyd_node **leader, struct lyd_node *node)
}

rb_remove_node(root_meta, &rbt, node, &removed);
rb_free_node(removed);
rb_free_node(&removed);
}

void
Expand Down Expand Up @@ -1383,15 +1388,15 @@ lyds_split(struct lyd_node **first_sibling, struct lyd_node *leader, struct lyd_
if (!start || (start->schema != node->schema)) {
/* @p node is the last node, remove from Red-black tree and unlink */
rb_remove_node(root_meta, &rbt, node, &rbn);
rb_free_node(rbn);
rb_free_node(&rbn);
lyd_unlink_ignore_lyds(first_sibling, node);
*next_p = start;
goto cleanup;
}

/* remove @p node from Red-black tree and unlink */
rb_remove_node(root_meta, &rbt, node, &rbn);
rb_free_node(rbn);
rb_free_node(&rbn);
lyd_unlink_ignore_lyds(first_sibling, node);

/* remove the rest of nodes from Red-black tree and unlink */
Expand All @@ -1401,7 +1406,7 @@ lyds_split(struct lyd_node **first_sibling, struct lyd_node *leader, struct lyd_
break;
}
rb_remove_node(root_meta, &rbt, iter, &rbn);
rb_free_node(rbn);
rb_free_node(&rbn);
lyd_unlink_ignore_lyds(first_sibling, iter);
/* insert them to the second (leaf-)list */
lyd_insert_after_node(&node, dst, iter);
Expand Down

0 comments on commit 09fd71d

Please sign in to comment.