This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 23a104a19f3716599ad17f07c97480eb688f1111 Author: yinshengkai <[email protected]> AuthorDate: Wed Jun 18 17:47:15 2025 +0800 libc: backtrace pool added robustness check Catch a corrupted pool or a bad record early: - backtrace_alloc() gets the entry from its free list node with container_of() instead of a cast, and asserts that it is unused. - backtrace_remove() asserts that the record is referenced before dropping the reference, and that a record whose last reference is dropped is found in its hash chain. Signed-off-by: yinshengkai <[email protected]> --- libs/libc/misc/lib_backtrace.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/libs/libc/misc/lib_backtrace.c b/libs/libc/misc/lib_backtrace.c index 4c3ab140fa9..5e8b119b093 100644 --- a/libs/libc/misc/lib_backtrace.c +++ b/libs/libc/misc/lib_backtrace.c @@ -25,12 +25,14 @@ ****************************************************************************/ #include <arch/irq.h> +#include <debug.h> #include <execinfo.h> #include <stdio.h> #include <stdbool.h> #include <syslog.h> #include <nuttx/init.h> +#include <nuttx/nuttx.h> #include <nuttx/sched.h> #include <nuttx/spinlock.h> @@ -286,6 +288,9 @@ backtrace_alloc(FAR struct backtrace_pool_s *bp, uint32_t slot, return NULL; } + entry = container_of(entry, struct backtrace_entry_s, freenode); + DEBUGASSERT(entry->ref == 0); + /* Copy the backtrace to the entry */ bp->used++; @@ -395,9 +400,10 @@ void backtrace_remove(FAR void *index) } flags = backtrace_lock(bp); - if (entry->ref > 1) + DEBUGASSERT(entry->ref > 0); + entry->ref--; + if (entry->ref > 0) { - entry->ref--; backtrace_unlock(bp, flags); return; } @@ -424,6 +430,12 @@ void backtrace_remove(FAR void *index) prev = prev->next; } + + /* If the backtrace record is not found, it means that the backtrace + * record entry is an invalid value. + */ + + DEBUGASSERT(prev != NULL); } bp->used--;
