Skip to content

Commit 9fcb609

Browse files
committed
Safer id management in importOSM...
1 parent 2e6d346 commit 9fcb609

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/dsm/headers/Graph.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ namespace dsm {
330330
std::getline(iss, lon, ';');
331331
std::getline(iss, lat, ';');
332332
std::getline(iss, highway, ';');
333-
auto const nodeId{static_cast<Id>(std::stoul(id))};
334333
if (highway.find("traffic_signals") != std::string::npos) {
335334
addNode<TrafficLight>(
336335
nodeIndex, 60, std::make_pair(std::stod(lat), std::stod(lon)));
@@ -340,7 +339,7 @@ namespace dsm {
340339
addNode<Intersection>(nodeIndex,
341340
std::make_pair(std::stod(lat), std::stod(lon)));
342341
}
343-
m_nodeMapping.emplace(std::make_pair(nodeId, nodeIndex));
342+
m_nodeMapping.emplace(std::make_pair(id, nodeIndex));
344343
++nodeIndex;
345344
}
346345
} else {
@@ -394,7 +393,7 @@ namespace dsm {
394393
lanes = "1"; // Default to 1 lane if lanes is invalid
395394
}
396395
}
397-
if (!m_nodeMapping.contains(std::stoul(sourceId))) {
396+
if (!m_nodeMapping.contains(sourceId)) {
398397
std::cerr << std::format(
399398
"\033[38;5;196mERROR ({}:{}): Node with id {} not "
400399
"found.\033[0m",
@@ -404,7 +403,7 @@ namespace dsm {
404403
<< std::endl;
405404
std::abort();
406405
}
407-
if (!m_nodeMapping.contains(std::stoul(targetId))) {
406+
if (!m_nodeMapping.contains(targetId)) {
408407
std::cerr << std::format(
409408
"\033[38;5;196mERROR ({}:{}): Node with id {} not "
410409
"found.\033[0m",
@@ -414,8 +413,15 @@ namespace dsm {
414413
<< std::endl;
415414
std::abort();
416415
}
417-
auto const srcId{m_nodeMapping.at(std::stoul(sourceId))};
418-
auto const dstId{m_nodeMapping.at(std::stoul(targetId))};
416+
auto const srcId{m_nodeMapping.at(sourceId)};
417+
auto const dstId{m_nodeMapping.at(targetId)};
418+
if (static_cast<unsigned long long>(srcId * nNodes + dstId) >
419+
std::numeric_limits<Id>::max()) {
420+
throw std::invalid_argument(buildLog(
421+
std::format("Street id {}->{} would too large for the current type of Id.",
422+
srcId,
423+
dstId)));
424+
}
419425
Id streetId = srcId * nNodes + dstId;
420426
addEdge<Street>(streetId,
421427
std::make_pair(srcId, dstId),

src/dsm/headers/Graph.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace dsm {
4949
private:
5050
std::unordered_map<Id, std::unique_ptr<Node>> m_nodes;
5151
std::unordered_map<Id, std::unique_ptr<Street>> m_streets;
52-
std::unordered_map<Id, Id> m_nodeMapping;
52+
std::unordered_map<std::string, Id> m_nodeMapping;
5353
SparseMatrix<bool> m_adjacency;
5454
unsigned long long m_maxAgentCapacity;
5555

0 commit comments

Comments
 (0)