Problem: Active poller double add mutates handler

Solution: Check if socket already added before storing.
This commit is contained in:
Gudmundur Adalsteinsson
2020-04-19 14:01:43 +00:00
parent 3e3fe85b33
commit d237615a25
2 changed files with 22 additions and 18 deletions

View File

@@ -675,20 +675,17 @@ class active_poller_t
{
if (!handler)
throw std::invalid_argument("null handler in active_poller_t::add");
auto it = decltype(handlers)::iterator{};
auto inserted = bool{};
std::tie(it, inserted) = handlers.emplace(
auto ret = handlers.emplace(
socket, std::make_shared<handler_type>(std::move(handler)));
if (!ret.second)
throw error_t(EINVAL); // already added
try {
base_poller.add(socket, events,
inserted && *(it->second) ? it->second.get() : nullptr);
need_rebuild |= inserted;
base_poller.add(socket, events, ret.first->second.get());
need_rebuild = true;
}
catch (const zmq::error_t &) {
catch (...) {
// rollback
if (inserted) {
handlers.erase(socket);
}
handlers.erase(socket);
throw;
}
}
@@ -720,8 +717,8 @@ class active_poller_t
std::for_each(poller_events.begin(),
poller_events.begin() + static_cast<ptrdiff_t>(count),
[](decltype(base_poller)::event_type &event) {
if (event.user_data != nullptr)
(*event.user_data)(event.events);
assert(event.user_data != nullptr);
(*event.user_data)(event.events);
});
return count;
}