mirror of
https://github.com/zeromq/cppzmq.git
synced 2025-04-27 18:30:54 +02:00
Problem: Active poller double add mutates handler
Solution: Check if socket already added before storing.
This commit is contained in:
parent
3e3fe85b33
commit
d237615a25
@ -14,9 +14,9 @@ TEST_CASE("create destroy", "[active_poller]")
|
|||||||
}
|
}
|
||||||
|
|
||||||
static_assert(!std::is_copy_constructible<zmq::active_poller_t>::value,
|
static_assert(!std::is_copy_constructible<zmq::active_poller_t>::value,
|
||||||
"active_active_poller_t should not be copy-constructible");
|
"active_poller_t should not be copy-constructible");
|
||||||
static_assert(!std::is_copy_assignable<zmq::active_poller_t>::value,
|
static_assert(!std::is_copy_assignable<zmq::active_poller_t>::value,
|
||||||
"active_active_poller_t should not be copy-assignable");
|
"active_poller_t should not be copy-assignable");
|
||||||
|
|
||||||
static const zmq::active_poller_t::handler_type no_op_handler =
|
static const zmq::active_poller_t::handler_type no_op_handler =
|
||||||
[](zmq::event_flags) {};
|
[](zmq::event_flags) {};
|
||||||
@ -115,12 +115,19 @@ TEST_CASE("add handler invalid events type", "[active_poller]")
|
|||||||
|
|
||||||
TEST_CASE("add handler twice throws", "[active_poller]")
|
TEST_CASE("add handler twice throws", "[active_poller]")
|
||||||
{
|
{
|
||||||
zmq::context_t context;
|
common_server_client_setup s;
|
||||||
zmq::socket_t socket{context, zmq::socket_type::router};
|
|
||||||
|
CHECK(s.client.send(zmq::message_t{}, zmq::send_flags::none));
|
||||||
|
|
||||||
zmq::active_poller_t active_poller;
|
zmq::active_poller_t active_poller;
|
||||||
active_poller.add(socket, zmq::event_flags::pollin, no_op_handler);
|
bool message_received = false;
|
||||||
|
active_poller.add(
|
||||||
|
s.server, zmq::event_flags::pollin,
|
||||||
|
[&message_received](zmq::event_flags) { message_received = true; });
|
||||||
CHECK_THROWS_ZMQ_ERROR(
|
CHECK_THROWS_ZMQ_ERROR(
|
||||||
EINVAL, active_poller.add(socket, zmq::event_flags::pollin, no_op_handler));
|
EINVAL, active_poller.add(s.server, zmq::event_flags::pollin, no_op_handler));
|
||||||
|
CHECK(1 == active_poller.wait(std::chrono::milliseconds{-1}));
|
||||||
|
CHECK(message_received); // handler unmodified
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("wait with no handlers throws", "[active_poller]")
|
TEST_CASE("wait with no handlers throws", "[active_poller]")
|
||||||
|
@ -675,20 +675,17 @@ class active_poller_t
|
|||||||
{
|
{
|
||||||
if (!handler)
|
if (!handler)
|
||||||
throw std::invalid_argument("null handler in active_poller_t::add");
|
throw std::invalid_argument("null handler in active_poller_t::add");
|
||||||
auto it = decltype(handlers)::iterator{};
|
auto ret = handlers.emplace(
|
||||||
auto inserted = bool{};
|
|
||||||
std::tie(it, inserted) = handlers.emplace(
|
|
||||||
socket, std::make_shared<handler_type>(std::move(handler)));
|
socket, std::make_shared<handler_type>(std::move(handler)));
|
||||||
|
if (!ret.second)
|
||||||
|
throw error_t(EINVAL); // already added
|
||||||
try {
|
try {
|
||||||
base_poller.add(socket, events,
|
base_poller.add(socket, events, ret.first->second.get());
|
||||||
inserted && *(it->second) ? it->second.get() : nullptr);
|
need_rebuild = true;
|
||||||
need_rebuild |= inserted;
|
|
||||||
}
|
}
|
||||||
catch (const zmq::error_t &) {
|
catch (...) {
|
||||||
// rollback
|
// rollback
|
||||||
if (inserted) {
|
handlers.erase(socket);
|
||||||
handlers.erase(socket);
|
|
||||||
}
|
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -720,8 +717,8 @@ class active_poller_t
|
|||||||
std::for_each(poller_events.begin(),
|
std::for_each(poller_events.begin(),
|
||||||
poller_events.begin() + static_cast<ptrdiff_t>(count),
|
poller_events.begin() + static_cast<ptrdiff_t>(count),
|
||||||
[](decltype(base_poller)::event_type &event) {
|
[](decltype(base_poller)::event_type &event) {
|
||||||
if (event.user_data != nullptr)
|
assert(event.user_data != nullptr);
|
||||||
(*event.user_data)(event.events);
|
(*event.user_data)(event.events);
|
||||||
});
|
});
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user