From 8acd1c14d620f4e4b716f195346431a6e3b753ab Mon Sep 17 00:00:00 2001 From: Michael McAllister Date: Tue, 21 Jul 2026 22:08:24 +0100 Subject: [PATCH] kernel: avoid divide-by-zero in thread_get_debug_info() for zero-size stacks thread_get_debug_info() computes the current stack usage as stack_used_current * 100 / thread->stack_size with no guard against a zero stack size. On hosted targets the main thread has stack_size == 0, so opening the "View OS stacks" debug screen divides by zero and panics with a floating point exception. Guard the division and report 0% when the stack size is unknown, which matches the stack_usage() helper. Likely affects all hosted non-SDL targets, but tested/confirmed on my HiBy R1. Change-Id: I8ecc56f53f1e97e8a59ca71c0e08a66093fecbae --- firmware/kernel/thread-common.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/firmware/kernel/thread-common.c b/firmware/kernel/thread-common.c index 0170b66237..7fd071b026 100644 --- a/firmware/kernel/thread-common.c +++ b/firmware/kernel/thread-common.c @@ -309,7 +309,10 @@ int thread_get_debug_info(unsigned int thread_id, size_t stack_used_current = thread->stack_size - (thread->context.sp - (uintptr_t)thread->stack); - infop->stack_usage_cur = stack_used_current * 100 / thread->stack_size; + /* Hosted implementations typically have the main stack managed + by the OS, so we create a fake entry with 0 length */ + infop->stack_usage_cur = thread->stack_size ? + stack_used_current * 100 / thread->stack_size : 0; #endif #if NUM_CORES > 1 infop->core = thread->core;