This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit a7b7630fd3046e1158713f56e8fdd2a472273170 Author: fangxinyong <[email protected]> AuthorDate: Thu Aug 21 17:58:06 2025 +0800 sched/task: task_setup return path cleanup Refactor nxtask_setup_stackargs() to use a single ret exit path. Avoid partially-initialized state on error paths and improve readability. Addresses Coverity HIS_metric_violation: RETURN. Signed-off-by: fangxinyong <[email protected]> --- sched/task/task_setup.c | 126 ++++++++++++++++++++++++++---------------------- 1 file changed, 69 insertions(+), 57 deletions(-) diff --git a/sched/task/task_setup.c b/sched/task/task_setup.c index 5c9cd83e764..d2f3b0d8189 100644 --- a/sched/task/task_setup.c +++ b/sched/task/task_setup.c @@ -529,12 +529,13 @@ int nxtask_setup_stackargs(FAR struct tcb_s *tcb, int nbytes; int argc; int i; + int ret = OK; /* Give a name to the unnamed tasks */ if (!name) { - name = (FAR char *)g_noname; + name = g_noname; } /* Get the size of the task name (including the NUL terminator) */ @@ -562,80 +563,91 @@ int nxtask_setup_stackargs(FAR struct tcb_s *tcb, DEBUGASSERT(strtablen < tcb->adj_stack_size); if (strtablen >= tcb->adj_stack_size) { - return -ENAMETOOLONG; + ret = -ENAMETOOLONG; } - - /* Increment the number of args. Here is a sanity check to - * prevent running away with an unterminated argv[] list. - * MAX_STACK_ARGS should be sufficiently large that this never - * happens in normal usage. - */ - - DEBUGASSERT(argc <= MAX_STACK_ARGS); - if (++argc > MAX_STACK_ARGS) + else { - return -E2BIG; + /* Increment the number of args. Here is a sanity check to + * prevent running away with an unterminated argv[] list. + * MAX_STACK_ARGS should be sufficiently large that this never + * happens in normal usage. + */ + + DEBUGASSERT(argc <= MAX_STACK_ARGS); + if (++argc > MAX_STACK_ARGS) + { + ret = -E2BIG; + } } } } - /* Allocate a stack frame to hold argv[] array and the strings. NOTE - * that argc + 2 entries are needed: The number of arguments plus the - * task name plus a NULL argv[] entry to terminate the list. - */ - - argvlen = (argc + 2) * sizeof(FAR char *); - stackargv = (FAR char **)up_stack_frame(tcb, argvlen + strtablen); - - DEBUGASSERT(stackargv != NULL); - if (stackargv == NULL) + if (ret == OK) { - return -ENOMEM; - } + /* Allocate a stack frame to hold argv[] array and the strings. NOTE + * that argc + 2 entries are needed: The number of arguments plus the + * task name plus a NULL argv[] entry to terminate the list. + */ - /* Get the address of the string table that will lie immediately after - * the argv[] array and mark it as a null string. - */ + argvlen = (argc + 2) * sizeof(FAR char *); + stackargv = (FAR char **)up_stack_frame(tcb, argvlen + strtablen); + + DEBUGASSERT(stackargv != NULL); + if (stackargv == NULL) + { + ret = -ENOMEM; + } + else + { + /* Get the address of the string table that will lie immediately + * after the argv[] array and mark it as a null string. + */ - str = (FAR char *)stackargv + argvlen; + str = (FAR char *)stackargv + argvlen; - /* Copy the task name. Increment str to skip over the task name and its - * NUL terminator in the string buffer. - */ + /* Copy the task name. Increment str to skip over the task name + * and its NUL terminator in the string buffer. + */ - stackargv[0] = str; - nbytes = strlen(name) + 1; - strlcpy(str, name, strtablen); - str += nbytes; - strtablen -= nbytes; + stackargv[0] = str; + nbytes = strlen(name) + 1; + strlcpy(str, name, strtablen); + str += nbytes; + strtablen -= nbytes; - /* Copy each argument */ + /* Copy each argument */ - for (i = 0; i < argc; i++) - { - /* Save the pointer to the location in the string buffer and copy - * the argument into the buffer. Increment str to skip over the - * argument and its NUL terminator in the string buffer. - */ + for (i = 0; i < argc; i++) + { + /* Save the pointer to the location in the string buffer and + * copy the argument into the buffer. Increment str to skip + * over the argument and its NUL terminator in the string + * buffer. + */ + + stackargv[i + 1] = str; + nbytes = strlen(argv[i]) + 1; + strlcpy(str, argv[i], strtablen); + str += nbytes; + strtablen -= nbytes; + } - stackargv[i + 1] = str; - nbytes = strlen(argv[i]) + 1; - strlcpy(str, argv[i], strtablen); - str += nbytes; - strtablen -= nbytes; - } + /* Put a terminator entry at the end of the argv[] array. Then + * save the argv[] array pointer in the TCB where it will be + * recovered later by nxtask_start(). + */ - /* Put a terminator entry at the end of the argv[] array. Then save the - * argv[] array pointer in the TCB where it will be recovered later by - * nxtask_start(). - */ + stackargv[argc + 1] = NULL; - stackargv[argc + 1] = NULL; + /* Initialize argv last to avoid accessing the partial initialized + * fields + */ - /* Initialize argv last to avoid accessing the partial initialized fields */ + nxsched_get_tls(tcb)->tl_argv = stackargv; + } + } - nxsched_get_tls(tcb)->tl_argv = stackargv; - return OK; + return ret; } /****************************************************************************
