Skip to content

Commit

Permalink
Small rz_graph_del_node fixups:
Browse files Browse the repository at this point in the history
- Add doc string.
- Check NULL graph.
- Check for node not part of graph.
  • Loading branch information
Rot127 committed Nov 27, 2023
1 parent 890ba28 commit d034af0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions librz/util/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,17 @@ RZ_API RzGraphNode *rz_graph_add_nodef(RzGraph *graph, void *data, RzListFree us
return node;
}

/* remove the node from the graph and free the node */
/* users of this function should be aware they can't access n anymore */
/**
* \brief Deletes the node \p n from the graph \p t and frees the \p n.
*
* \param t The graph to operate on.
* \param n The node to delete.
*/
RZ_API void rz_graph_del_node(RzGraph *t, RZ_OWN RzGraphNode *n) {
rz_return_if_fail(t);
RzGraphNode *gn;
RzListIter *it;
if (!n) {
if (!n || !rz_list_contains(t->nodes, n)) {
return;
}
rz_list_foreach (n->in_nodes, it, gn) {
Expand Down
5 changes: 5 additions & 0 deletions test/unit/test_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ static bool test_legacy_graph(void) {
mu_assert_eq(g->n_nodes, 8, "n_nodes.del_node");
mu_assert_eq(g->n_edges, 12, "n_edges.del_node");

// Test invalid removal
rz_graph_del_node(g, NULL);
RzGraphNode dummy = { 0 };
rz_graph_del_node(g, &dummy);

rz_graph_free(g);
mu_end;
}
Expand Down

0 comments on commit d034af0

Please sign in to comment.