From 2394413ec3c67ffe6e0404352c2d9f5e6a737c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Tue, 23 Aug 2022 23:47:52 +0200 Subject: [PATCH] hook: don't remove from unitialized list A lot of code calls spa_hook_remove() from error paths where the hook and therefore the list may not have been initialized. This leads to null-derefences. --- spa/include/spa/utils/hook.h | 3 ++- test/test-spa-utils.c | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/spa/include/spa/utils/hook.h b/spa/include/spa/utils/hook.h index 9b1a50b63..953b97445 100644 --- a/spa/include/spa/utils/hook.h +++ b/spa/include/spa/utils/hook.h @@ -382,7 +382,8 @@ static inline void spa_hook_list_prepend(struct spa_hook_list *list, /** Remove a hook */ static inline void spa_hook_remove(struct spa_hook *hook) { - spa_list_remove(&hook->link); + if (spa_list_is_initialized(&hook->link)) + spa_list_remove(&hook->link); if (hook->removed) hook->removed(hook); } diff --git a/test/test-spa-utils.c b/test/test-spa-utils.c index 0a750391d..2f198f90a 100644 --- a/test/test-spa-utils.c +++ b/test/test-spa-utils.c @@ -419,6 +419,12 @@ PWTEST(utils_hook) } pwtest_int_eq(count, 4); pwtest_int_eq(hook_free_count, 4); + + /* remove a zeroed hook */ + struct spa_hook hook; + spa_zero(hook); + spa_hook_remove(&hook); + return PWTEST_PASS; }