From: Davin McCall Date: Sat, 10 Feb 2018 15:32:51 +0000 (+0000) Subject: control conn. allocate_service_handle(): exception safety. X-Git-Tag: v0.1.0~35 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=2d2998c08de980bb7f8b6a1ad5cda0a16fcd563d;p=oweals%2Fdinit.git control conn. allocate_service_handle(): exception safety. --- diff --git a/src/control.cc b/src/control.cc index 85a70ec..fd89e20 100644 --- a/src/control.cc +++ b/src/control.cc @@ -292,6 +292,8 @@ bool control_conn_t::list_services() control_conn_t::handle_t control_conn_t::allocate_service_handle(service_record *record) { + // Try to find a unique handle (integer) in a single pass. Start with 0, and keep track of the largest + // handle seen so far. If we find the current candidate, we set the candidate to (largest+1). bool is_unique = true; handle_t largest_seen = 0; handle_t candidate = 0; @@ -303,14 +305,25 @@ control_conn_t::handle_t control_conn_t::allocate_service_handle(service_record } is_unique &= (p.second != record); } - - keyServiceMap[candidate] = record; - serviceKeyMap.insert(std::make_pair(record, candidate)); - + + // The following operations perform allocation (can throw std::bad_alloc). If an exception occurs we + // must undo any previous actions: if (is_unique) { record->add_listener(this); } + try { + keyServiceMap[candidate] = record; + serviceKeyMap.insert(std::make_pair(record, candidate)); + } + catch (...) { + if (is_unique) { + record->remove_listener(this); + } + + keyServiceMap.erase(candidate); + } + return candidate; }