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 f4b44301c944dc70be67dae95d9daaddc0c38c8b Author: vela-autotest04 <[email protected]> AuthorDate: Tue May 5 02:51:58 2026 +0800 libc/regex: Avoid the O(N) stack depth in tre_ast_to_tnfa(). Root cause: tre_ast_to_tnfa() recursed on every AST child, including the final (tail) edge of UNION, CATENATION and ITERATION nodes. For a simple literal pattern such as "test_nuttx_syscall_fsync01" the TRE parser produces a right-leaning AST of CATENATION nodes, so the recursion depth scales linearly with the regex length. With KASAN enabled the resulting per-frame overhead is enough to eat an 8 KB task stack (CONFIG_DEFAULT_TASK_STACKSIZE) and trip the CONFIG_STACKCHECK_MARGIN guard in nxsched_checkstackoverflow(), crashing the cmocka_syscall_test task during regcomp(). Fix: keep the existing traversal semantics but drive the last recursive call of each node type through a for-loop, so tail calls no longer consume stack. Only the non-tail recursion for the left child of UNION/CATENATION remains, which is bounded by the balanced depth of the AST rather than its total size. This eliminates the TODO noted in the original source ("XXX - recurse using a stack!"). Signed-off-by: dengwenqi <[email protected]> Signed-off-by: ganjing <[email protected]> --- libs/libc/regex/regcomp.c | 129 +++++++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 58 deletions(-) diff --git a/libs/libc/regex/regcomp.c b/libs/libc/regex/regcomp.c index 37ac6c2135d..2e90cec41de 100644 --- a/libs/libc/regex/regcomp.c +++ b/libs/libc/regex/regcomp.c @@ -3566,80 +3566,93 @@ static reg_errcode_t tre_ast_to_tnfa(tre_ast_node_t *node, tre_iteration_t *iter; reg_errcode_t errcode = REG_OK; - /* XXX - recurse using a stack!. */ - - switch (node->type) - { - case LITERAL: - { - } - break; + /* Walk the AST iteratively: the last recursive call in each branch is + * turned into a loop step so the stack depth does not grow with the + * regex length. A naive recursive walk produced one frame per AST + * node, which could overflow small task stacks for long literal + * concatenations (the AST is right-leaning, e.g. "abcdef" becomes + * CAT(a, CAT(b, CAT(c, ...)))). + */ - case UNION: + for (; ; ) { - uni = (tre_union_t *)node->obj; - errcode = tre_ast_to_tnfa(uni->left, transitions, counts, offs); - if (errcode != REG_OK) + switch (node->type) { - return errcode; - } + case LITERAL: + return errcode; - errcode = tre_ast_to_tnfa(uni->right, transitions, counts, offs); - } - break; + case UNION: + uni = (tre_union_t *)node->obj; + errcode = tre_ast_to_tnfa(uni->left, transitions, counts, offs); + if (errcode != REG_OK) + { + return errcode; + } - case CATENATION: - { - cat = (tre_catenation_t *)node->obj; + /* Tail call: continue with uni->right without growing stack. */ - /* Add a transition from each position in cat->left->lastpos - * to each position in cat->right->firstpos. - */ + node = uni->right; + continue; - errcode = tre_make_trans(cat->left->lastpos, cat->right->firstpos, - transitions, counts, offs); - if (errcode != REG_OK) - { - return errcode; - } + case CATENATION: + cat = (tre_catenation_t *)node->obj; - errcode = tre_ast_to_tnfa(cat->left, transitions, counts, offs); - if (errcode != REG_OK) - { - return errcode; - } + /* Add a transition from each position in cat->left->lastpos + * to each position in cat->right->firstpos. + */ - errcode = tre_ast_to_tnfa(cat->right, transitions, counts, offs); - } - break; + errcode = tre_make_trans(cat->left->lastpos, + cat->right->firstpos, + transitions, counts, offs); + if (errcode != REG_OK) + { + return errcode; + } - case ITERATION: - { - iter = (tre_iteration_t *)node->obj; - ASSERT(iter->max == -1 || iter->max == 1); + errcode = tre_ast_to_tnfa(cat->left, transitions, counts, offs); + if (errcode != REG_OK) + { + return errcode; + } - if (iter->max == -1) - { - ASSERT(iter->min == 0 || iter->min == 1); + /* Tail call: continue with cat->right without growing stack. */ - /* Add a transition from each last position in the iterated - * expression to each first position. - */ + node = cat->right; + continue; - errcode = tre_make_trans(iter->arg->lastpos, iter->arg->firstpos, - transitions, counts, offs); - if (errcode != REG_OK) - { - return errcode; - } + case ITERATION: + iter = (tre_iteration_t *)node->obj; + ASSERT(iter->max == -1 || iter->max == 1); + + if (iter->max == -1) + { + ASSERT(iter->min == 0 || iter->min == 1); + + /* Add a transition from each last position in the iterated + * expression to each first position. + */ + + errcode = tre_make_trans(iter->arg->lastpos, + iter->arg->firstpos, + transitions, counts, offs); + if (errcode != REG_OK) + { + return errcode; + } + } + + /* Tail call: continue with iter->arg without growing stack. */ + + node = iter->arg; + continue; } - errcode = tre_ast_to_tnfa(iter->arg, transitions, counts, offs); - } - break; - } + /* Unknown node type: keep the original behaviour of returning + * REG_OK. + */ - return errcode; + return errcode; + } } #define ERROR_EXIT(err) \
