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 <fangxinyong@xiaomi.com>
This commit is contained in:
fangxinyong
2025-08-21 17:58:06 +08:00
committed by Xiang Xiao
parent 31901573ba
commit a7b7630fd3
+30 -18
View File
@@ -529,12 +529,13 @@ int nxtask_setup_stackargs(FAR struct tcb_s *tcb,
int nbytes; int nbytes;
int argc; int argc;
int i; int i;
int ret = OK;
/* Give a name to the unnamed tasks */ /* Give a name to the unnamed tasks */
if (!name) if (!name)
{ {
name = (FAR char *)g_noname; name = g_noname;
} }
/* Get the size of the task name (including the NUL terminator) */ /* Get the size of the task name (including the NUL terminator) */
@@ -562,9 +563,10 @@ int nxtask_setup_stackargs(FAR struct tcb_s *tcb,
DEBUGASSERT(strtablen < tcb->adj_stack_size); DEBUGASSERT(strtablen < tcb->adj_stack_size);
if (strtablen >= tcb->adj_stack_size) if (strtablen >= tcb->adj_stack_size)
{ {
return -ENAMETOOLONG; ret = -ENAMETOOLONG;
} }
else
{
/* Increment the number of args. Here is a sanity check to /* Increment the number of args. Here is a sanity check to
* prevent running away with an unterminated argv[] list. * prevent running away with an unterminated argv[] list.
* MAX_STACK_ARGS should be sufficiently large that this never * MAX_STACK_ARGS should be sufficiently large that this never
@@ -574,11 +576,14 @@ int nxtask_setup_stackargs(FAR struct tcb_s *tcb,
DEBUGASSERT(argc <= MAX_STACK_ARGS); DEBUGASSERT(argc <= MAX_STACK_ARGS);
if (++argc > MAX_STACK_ARGS) if (++argc > MAX_STACK_ARGS)
{ {
return -E2BIG; ret = -E2BIG;
}
} }
} }
} }
if (ret == OK)
{
/* Allocate a stack frame to hold argv[] array and the strings. NOTE /* Allocate a stack frame to hold argv[] array and the strings. NOTE
* that argc + 2 entries are needed: The number of arguments plus the * that argc + 2 entries are needed: The number of arguments plus the
* task name plus a NULL argv[] entry to terminate the list. * task name plus a NULL argv[] entry to terminate the list.
@@ -590,17 +595,18 @@ int nxtask_setup_stackargs(FAR struct tcb_s *tcb,
DEBUGASSERT(stackargv != NULL); DEBUGASSERT(stackargv != NULL);
if (stackargv == NULL) if (stackargv == NULL)
{ {
return -ENOMEM; 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. /* 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 /* Copy the task name. Increment str to skip over the task name
* NUL terminator in the string buffer. * and its NUL terminator in the string buffer.
*/ */
stackargv[0] = str; stackargv[0] = str;
@@ -613,9 +619,10 @@ int nxtask_setup_stackargs(FAR struct tcb_s *tcb,
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
{ {
/* Save the pointer to the location in the string buffer and copy /* Save the pointer to the location in the string buffer and
* the argument into the buffer. Increment str to skip over the * copy the argument into the buffer. Increment str to skip
* argument and its NUL terminator in the string buffer. * over the argument and its NUL terminator in the string
* buffer.
*/ */
stackargv[i + 1] = str; stackargv[i + 1] = str;
@@ -625,17 +632,22 @@ int nxtask_setup_stackargs(FAR struct tcb_s *tcb,
strtablen -= nbytes; strtablen -= nbytes;
} }
/* Put a terminator entry at the end of the argv[] array. Then save the /* Put a terminator entry at the end of the argv[] array. Then
* argv[] array pointer in the TCB where it will be recovered later by * save the argv[] array pointer in the TCB where it will be
* nxtask_start(). * 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;
} }
/**************************************************************************** /****************************************************************************