Skip to content

Commit

Permalink
pw_containers: Check that FlatMap it is not end() before dereferencing
Browse files Browse the repository at this point in the history
This bug results in asan errors when looking up keys that are larger
than the max key in the flat map.

Change-Id: Iad88f0138ff33b8b6f2ab6b10692ed7170d4fdfa
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/97801
Commit-Queue: Trinity Lundgren <[email protected]>
Reviewed-by: Wyatt Hepler <[email protected]>
  • Loading branch information
trinitylundgren authored and CQ Bot Account committed Jun 13, 2022
1 parent 7d7e7ad commit 6ee3471
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pw_containers/flat_map_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,13 @@ TEST(FlatMap, MapsWithUnsortedKeys) {
EXPECT_EQ(too_short.begin()->first, 0);
}

TEST(FlatMap, DontDereferenceEnd) {
constexpr FlatMap<int, const char*, 2> unsorted_array({{
{2, "hello"},
{1, "goodbye"},
}});

EXPECT_EQ(unsorted_array.contains(3), false);
}

} // namespace pw::containers
5 changes: 4 additions & 1 deletion pw_containers/public/pw_containers/flat_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ class FlatMap {
}

const_iterator it = lower_bound(key);
return key == it->first ? it : end();
if (it == end() || it->first != key) {
return end();
}
return it;
}

constexpr const_iterator lower_bound(const key_type& key) const {
Expand Down

0 comments on commit 6ee3471

Please sign in to comment.