From 783a8debb986dadf80e1e0260970c1aaf86b52ed Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Sun, 18 Jul 2021 14:14:41 +0200 Subject: [PATCH] posix: avoid use-after-free in signal handler --- platform/posix/btstack_signal.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/platform/posix/btstack_signal.c b/platform/posix/btstack_signal.c index 38324d7ed..e92af8d68 100644 --- a/platform/posix/btstack_signal.c +++ b/platform/posix/btstack_signal.c @@ -51,6 +51,12 @@ static void signal_callback(void * arg){ } static void * signal_thread(void *arg) { + // prepare registration + btstack_context_callback_registration_t registration; + memset(®istration, 0, sizeof(btstack_context_callback_registration_t)); + registration.callback = &signal_callback; + registration.context = arg; + while (1){ // wait for signal sigset_t sigset; @@ -60,10 +66,6 @@ static void * signal_thread(void *arg) { (void) sigwait(&sigset, &sig); // execute callback on main thread - btstack_context_callback_registration_t registration; - memset(®istration, 0, sizeof(btstack_context_callback_registration_t)); - registration.callback = &signal_callback; - registration.context = arg; btstack_run_loop_execute_on_main_thread(®istration); } return NULL;