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 b71974659f29d601e8d98f0f6da0920d6d7143af Author: ligd <[email protected]> AuthorDate: Tue Jan 6 17:36:29 2026 +0800 sched/dumpstack: Add max loop limit to prevent infinite loop When backtrace returns corrupted stack data, sched_dumpstack's for loop may iterate indefinitely since it continues as long as size == DUMP_DEPTH. Add DUMP_MAX_LOOPS (32) limit to cap the maximum iterations, allowing at most 512 stack frames to be dumped which is sufficient for debugging while preventing system hang. Signed-off-by: ligd <[email protected]> --- libs/libc/sched/sched_dumpstack.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libs/libc/sched/sched_dumpstack.c b/libs/libc/sched/sched_dumpstack.c index 2b61babc13a..6b5f0fa1c13 100644 --- a/libs/libc/sched/sched_dumpstack.c +++ b/libs/libc/sched/sched_dumpstack.c @@ -36,10 +36,11 @@ * Pre-processor Definitions ****************************************************************************/ -#define DUMP_DEPTH 16 -#define DUMP_NITEM 8 -#define DUMP_WIDTH (int)(2 * sizeof(FAR void *) + 2) -#define DUMP_LINESZ (DUMP_NITEM * (DUMP_WIDTH + 1)) +#define DUMP_DEPTH 16 +#define DUMP_NITEM 8 +#define DUMP_WIDTH (int)(2 * sizeof(FAR void *) + 2) +#define DUMP_LINESZ (DUMP_NITEM * (DUMP_WIDTH + 1)) +#define DUMP_MAX_LOOPS 32 /* Max iterations to prevent infinite loop */ /**************************************************************************** * Public Functions @@ -57,8 +58,10 @@ void sched_dumpstack(pid_t tid) { int size = DUMP_DEPTH; int skip; + int loops = 0; - for (skip = 0; size == DUMP_DEPTH; skip += size) + for (skip = 0; size == DUMP_DEPTH && loops++ < DUMP_MAX_LOOPS; + skip += size) { FAR void *address[DUMP_DEPTH]; #ifndef CONFIG_ALLSYMS
