Skip to content

Commit 0d16266

Browse files
committed
overflow check for graph edges
1 parent 36c0832 commit 0d16266

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

mt-kahypar/datastructures/dynamic_graph_factory.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include "dynamic_graph_factory.h"
3030

31+
#include <limits>
32+
3133
#include <tbb/parallel_for.h>
3234
#include <tbb/parallel_invoke.h>
3335

@@ -68,6 +70,13 @@ DynamicGraph DynamicGraphFactory::construct_from_graph_edges(
6870
const mt_kahypar_hyperedge_weight_t* edge_weight,
6971
const mt_kahypar_hypernode_weight_t* node_weight,
7072
const bool stable_construction_of_incident_edges) {
73+
if (num_edges > std::numeric_limits<HyperedgeID>::max() / 2) {
74+
std::string msg = std::string("number of edges overflows ID range (note: graph edges are duplicated, require +1 bit)");
75+
if constexpr (sizeof(HyperedgeID) < 8) {
76+
msg += "; build with -DKAHYPAR_USE_64_BIT_IDS=ON to support larger ID ranges";
77+
}
78+
throw InvalidInputException(msg);
79+
}
7180
if (edge_vector.size() != num_edges) {
7281
throw InvalidInputException("Number of edges does not match length of input data!");
7382
}

mt-kahypar/datastructures/static_graph_factory.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include "static_graph_factory.h"
3030

31+
#include <limits>
32+
3133
#include <tbb/parallel_for.h>
3234
#include <tbb/parallel_invoke.h>
3335

@@ -95,6 +97,13 @@ namespace mt_kahypar::ds {
9597
const mt_kahypar_hyperedge_weight_t* edge_weight,
9698
const mt_kahypar_hypernode_weight_t* node_weight,
9799
const bool stable_construction_of_incident_edges) {
100+
if (num_edges > std::numeric_limits<HyperedgeID>::max() / 2) {
101+
std::string msg = std::string("number of edges overflows ID range (note: graph edges are duplicated, require +1 bit)");
102+
if constexpr (sizeof(HyperedgeID) < 8) {
103+
msg += "; build with -DKAHYPAR_USE_64_BIT_IDS=ON to support larger ID ranges";
104+
}
105+
throw InvalidInputException(msg);
106+
}
98107
if (edge_vector.size() != num_edges) {
99108
throw InvalidInputException("Number of edges does not match length of input data!");
100109
}

0 commit comments

Comments
 (0)