serge-sans-paille / frozen

a header-only, constexpr alternative to gperf for C++14 users
Apache License 2.0
1.27k stars 103 forks source link

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

Open mikekaganski opened 1 year ago

mikekaganski commented 1 year ago

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.

muggenhor commented 12 months ago

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): https://github.com/serge-sans-paille/frozen/blob/5666e8cd770fa1fc6afbc3c24a36aad418332e73/include/frozen/map.h#L194-L201