Skip to content

Commit

Permalink
Prevent creation of node duplicates. (#4233)
Browse files Browse the repository at this point in the history
Because list-based graph implementation cannot differentiate nodes by their data,
node duplicates are added, if we don't take care of it.
  • Loading branch information
Rot127 authored Feb 15, 2024
1 parent 0867fd9 commit 7558233
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions librz/core/cgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -993,12 +993,19 @@ static bool add_edge_to_cfg(RZ_NONNULL RzGraph /*<RzGraphNodeInfo *>*/ *graph,
return false;
}

RzGraphNode *to_node = add_node_info_cfg(graph, op_to, false);
RzGraphNode *to_node = NULL;
bool found = false;
ut64 to_idx = ht_uu_find(nodes_visited, to, &found);
if (found) {
to_node = rz_graph_get_node(graph, to_idx);
} else {
to_node = add_node_info_cfg(graph, op_to, false);
}
if (!to_node) {
RZ_LOG_ERROR("Could not add node at 0x%" PFMT64x "\n", to);
return false;
}
ut64 to_idx = to_node->idx;
to_idx = to_node->idx;
if (from == to) {
from_idx = to_idx;
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/test_analysis_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ bool test_analysis_graph_cfg() {

RzGraph *g = rz_core_graph_cfg(core, 0x117a); // main()
mu_assert_notnull(g, "Graph was NULL");
mu_assert_eq(g->n_nodes, 26, "data graph node count");
mu_assert_eq(g->n_nodes, 24, "data graph node count");
mu_assert_eq(g->n_edges, 25, "data graph edge count");

// Testing the node content is a little annoying. The nodes
Expand Down Expand Up @@ -291,7 +291,7 @@ bool test_analysis_graph_cfg() {
mu_assert_eq(info->cfg.address, 0x11a7, "info address");
mu_assert_eq(info->cfg.call_address, UT64_MAX, "info call address");

info = rz_graph_get_node_info_data(rz_graph_get_node(g, 24)->data);
info = rz_graph_get_node_info_data(rz_graph_get_node(g, 23)->data);
mu_assert_eq(info->type, RZ_GRAPH_NODE_TYPE_CFG, "info type");
mu_assert_eq(info->subtype, RZ_GRAPH_NODE_SUBTYPE_CFG_CALL, "info subtype");
mu_assert_eq(info->cfg.address, 0x11cd, "info address");
Expand Down

0 comments on commit 7558233

Please sign in to comment.