Use lower_bound() before iterating multimap entries of a key (#1776)

Several places look up a multimap with find(key) and then iterate while
iter->first == key, assuming find() returns the first element with that key.
The standard does not guarantee this, and recent libc++ (Apple clang 21 /
libc++ 2200) returns an arbitrary matching element. graph::findLink() then
misses existing links and Optimizer::getConnectedGraph() aborts with
"Condition (kter!=linksIn.end()) not met!" on graphs with loop closures or
multiple sessions (rtabmap-export --opt 0, rtabmap-reprocess, etc.).

Replace find() with lower_bound() at those sites and add a regression test.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Co-authored-by: matlabbe <[email protected]>
This commit is contained in:
Nathan Totten
2026-09-27 18:37:17 -07:00
committed by GitHub
co-authored by Claude Opus 5.5 matlabbe
parent 8035be52ff
commit 5c9cfa98fe
6 changed files with 46 additions and 23 deletions
+23
View File
@@ -124,6 +124,29 @@ TEST(GraphTest, FindLinkForwardAndReverse)
EXPECT_NE(graph::findLink(links, 1, 2, true, Link::kNeighbor), links.end());
}
TEST(GraphTest, FindLinkWithManyLinksPerNode)
{
// multimap::find() may return any element with the key (recent libc++ does),
// so lookups must start from lower_bound() to see every link of a node.
std::multimap<int, Link> links;
std::multimap<int, std::pair<int, Link::Type> > biLinks;
std::multimap<int, int> intLinks;
for(int to=2; to<=40; ++to)
{
insertLink(links, Link(1, to, to%2?Link::kGlobalClosure:Link::kNeighbor, Transform::getIdentity()));
biLinks.insert(std::make_pair(1, std::make_pair(to, to%2?Link::kGlobalClosure:Link::kNeighbor)));
intLinks.insert(std::make_pair(1, to));
}
for(int to=2; to<=40; ++to)
{
Link::Type type = to%2?Link::kGlobalClosure:Link::kNeighbor;
EXPECT_NE(graph::findLink(links, 1, to, false, type), links.end()) << "to=" << to;
EXPECT_NE(graph::findLink(links, to, 1, true, type), links.end()) << "to=" << to;
EXPECT_NE(graph::findLink(biLinks, 1, to, false, type), biLinks.end()) << "to=" << to;
EXPECT_NE(graph::findLink(intLinks, 1, to), intLinks.end()) << "to=" << to;
}
}
TEST(GraphTest, FindLinkIntMultimap)
{
std::multimap<int, int> links;