From 6c5fdac5b534cf483ad382a5c13ca4801ba4ba7c Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Mon, 2 Jul 2018 17:26:26 +0100 Subject: [PATCH] service: correctly roll-back state on exception (bad_alloc). Since a service adds its dependencies to other services' dependents lists, it needs to roll back those changes if failing due to a bad_alloc (or other exception). --- src/includes/service.h | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/includes/service.h b/src/includes/service.h index 0c4b0cc..0d3acc7 100644 --- a/src/includes/service.h +++ b/src/includes/service.h @@ -482,9 +482,24 @@ class service_record service_name = name; this->record_type = record_type_p; - for (auto & pdep : deplist_p) { - auto b = depends_on.emplace(depends_on.end(), this, pdep.to, pdep.dep_type); - pdep.to->dependents.push_back(&(*b)); + try { + for (auto & pdep : deplist_p) { + auto b = depends_on.emplace(depends_on.end(), this, pdep.to, pdep.dep_type); + try { + pdep.to->dependents.push_back(&(*b)); + } + catch (...) { + // we'll roll back one now and re-throw: + depends_on.pop_back(); + throw; + } + } + } + catch (...) { + for (auto & dep : depends_on) { + dep.get_to()->dependents.pop_back(); + } + throw; } } -- 2.25.1