{1:00} fixed possible crash in AMQP::Reliable when more messages are published as result of an incoming ack/nack (the iterator in a map could be invalidated by the call back to user space)

This commit is contained in:
Emiel Bruijntjes 2023-06-22 19:47:01 +02:00
parent d87343afbc
commit 54b625693c
1 changed files with 20 additions and 15 deletions

View File

@ -67,34 +67,39 @@ private:
// the base-class publish methods for some reason. // the base-class publish methods for some reason.
if (iter == _handlers.end()) return BASE::onAck(deliveryTag, multiple); if (iter == _handlers.end()) return BASE::onAck(deliveryTag, multiple);
// call the ack handler // get the handler (we store it first so that we can remove it)
iter->second->reportAck(); auto handler = iter->second;
// if the monitor is no longer valid, we stop (we're done) // erase it from the map (we remove it before the call, because the callback might update
if (!monitor) return; // the _handlers and invalidate the iterator)
// erase it from the map
_handlers.erase(iter); _handlers.erase(iter);
// call the ack handler
handler->reportAck();
} }
// do multiple at once // do multiple at once
else else
{ {
// call the handlers // keep looping for as long as the object is in a valid state
for (auto iter = _handlers.begin(); iter != _handlers.end(); iter++) while (monitor && !_handlers.empty())
{ {
// get the first handler
auto iter = _handlers.begin();
// make sure this is the right deliverytag, if we've passed it we leap out // make sure this is the right deliverytag, if we've passed it we leap out
if (iter->first > deliveryTag) break; if (iter->first > deliveryTag) break;
// call the handler // get the handler
iter->second->reportAck(); auto handler = iter->second;
// if we were destructed in the meantime, we leap out // remove it from the map
if (!monitor) return; _handlers.erase(iter);
// call the ack handler
handler->reportAck();
} }
// erase all acknowledged items
_handlers.erase(_handlers.begin(), _handlers.upper_bound(deliveryTag));
} }
// make sure the object is still valid // make sure the object is still valid