From 5a35037af37d7af3e98d5da1fc2e7d22421f5065 Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Sun, 26 May 2024 01:55:10 +0800 Subject: [PATCH] arch: riscv: check esf before calling z_riscv_unwind_stack Make sure that esf is not NULL before calling z_riscv_unwind_stack to prevent NULL pointer dereferencing. Signed-off-by: Yong Cong Sin --- arch/riscv/core/fatal.c | 2 +- arch/riscv/core/stacktrace.c | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/arch/riscv/core/fatal.c b/arch/riscv/core/fatal.c index 31ead071eb9..2624edbec90 100644 --- a/arch/riscv/core/fatal.c +++ b/arch/riscv/core/fatal.c @@ -107,7 +107,7 @@ FUNC_NORETURN void z_riscv_fatal_error_csf(unsigned int reason, const z_arch_esf LOG_ERR(""); } - if (IS_ENABLED(CONFIG_RISCV_EXCEPTION_STACK_TRACE)) { + if (IS_ENABLED(CONFIG_RISCV_EXCEPTION_STACK_TRACE) && (esf != NULL)) { z_riscv_unwind_stack(esf); } diff --git a/arch/riscv/core/stacktrace.c b/arch/riscv/core/stacktrace.c index e0c97b642ba..9c6c013efae 100644 --- a/arch/riscv/core/stacktrace.c +++ b/arch/riscv/core/stacktrace.c @@ -88,10 +88,6 @@ void z_riscv_unwind_stack(const z_arch_esf_t *esf) uintptr_t ra; struct stackframe *frame; - if (esf == NULL) { - return; - } - LOG_ERR("call trace:"); for (int i = 0; (i < MAX_STACK_FRAMES) && (fp != 0U) && in_stack_bound(fp);) { @@ -121,10 +117,6 @@ void z_riscv_unwind_stack(const z_arch_esf_t *esf) uintptr_t ra; uintptr_t *ksp = (uintptr_t *)sp; - if (esf == NULL) { - return; - } - LOG_ERR("call trace:"); for (int i = 0;