Skip to content

map::at() may return incorrect item #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mikekaganski opened this issue Jul 13, 2023 · 1 comment
Open

map::at() may return incorrect item #156

mikekaganski opened this issue Jul 13, 2023 · 1 comment

Comments

@mikekaganski
Copy link

mikekaganski commented Jul 13, 2023

map::at() is implemented using at_impl, which itself is:

  template <class This, class KeyType>
  static inline constexpr auto& at_impl(This&& self, KeyType const &key) {
    auto where = self.lower_bound(key);
    if (where != self.end())
      return where->second;
    else
      FROZEN_THROW_OR_ABORT(std::out_of_range("unknown key"));
  }

So whenever lower_bound succeeds, it returns the found value. But lower_bound returns whatever compares not less than - so a check is needed, that it is equal to? Possibly using find_impl.

@mikekaganski mikekaganski changed the title at() may return incorrect item map::at() may return incorrect item Jul 13, 2023
@muggenhor
Copy link
Contributor

What code exactly are you looking at? Because this looks fine to me by doing almost exactly what you suggest (deferring to find, though not find_impl):

frozen/include/frozen/map.h

Lines 194 to 201 in 5666e8c

template <class This, class KeyType>
static inline constexpr auto& at_impl(This&& self, KeyType const &key) {
auto where = self.find(key);
if (where != self.end())
return where->second;
else
FROZEN_THROW_OR_ABORT(std::out_of_range("unknown key"));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants