kernel: avoid divide-by-zero in thread_get_debug_info() for zero-size stacks

rockbox-gerrit-noreply--- via rockbox-cvs <[email protected]> Wed, 22 Jul 2026 09:34:45 -0400
Newsgroups gmane.comp.systems.archos.rockbox.cvs
Message-ID <[email protected]>
commit 8acd1c14d620f4e4b716f195346431a6e3b753ab
Author: Michael McAllister <[email protected]>
Date:   Tue Jul 21 22:08:24 2026 +0100

    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

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;
-- 
rockbox-cvs mailing list
[email protected]
https://lists.haxx.se/mailman/listinfo/rockbox-cvs