From 27f300b84a986401a97aaa19d203796ff049334c Mon Sep 17 00:00:00 2001 From: Max Ilsen Date: Thu, 30 Nov 2023 11:45:43 +0100 Subject: [PATCH] Fix dangling pointer The address of the local variable controller is still referenced to by the static variable controller_ after run() is executed and controller has been deallocated. --- bandit/controller.h | 8 ++++++++ bandit/runner.h | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/bandit/controller.h b/bandit/controller.h index fefb248..38b22a9 100644 --- a/bandit/controller.h +++ b/bandit/controller.h @@ -146,6 +146,10 @@ namespace bandit { get_controller_address() = controller; } + static void deregister_controller() { + get_controller_address() = nullptr; + } + static controller_t& registered_controller() { auto controller = get_controller_address(); throw_if_nullptr(controller, "controller", "bandit::detail::register_controller()"); @@ -201,6 +205,10 @@ namespace bandit { controller_t::register_controller(controller); } + inline void deregister_controller() { + controller_t::deregister_controller(); + } + inline controller_t& registered_controller() { return controller_t::registered_controller(); } diff --git a/bandit/runner.h b/bandit/runner.h index c4c2e57..d8262a7 100644 --- a/bandit/runner.h +++ b/bandit/runner.h @@ -110,7 +110,10 @@ namespace bandit { controller.set_policy(new run_policy::bandit(opt.filter_chain(), opt.break_on_failure(), opt.dry_run())); detail::register_controller(&controller); - return run(opt, detail::specs()); + int result = run(opt, detail::specs()); + detail::deregister_controller(); + + return result; } inline int run(int argc, char* argv[], bool allow_further = true) {