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
+76 -64
View File
@@ -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;
}
/* 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;
/* 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;
/* 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.
/* 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.
*/
stackargv[i + 1] = str;
nbytes = strlen(argv[i]) + 1;
strlcpy(str, argv[i], strtablen);
str += nbytes;
strtablen -= nbytes;
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;
/* 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;
/* 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.
*/
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().
*/
stackargv[argc + 1] = NULL;
/* Initialize argv last to avoid accessing the partial initialized
* fields
*/
nxsched_get_tls(tcb)->tl_argv = stackargv;
}
}
/* 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;
/* Initialize argv last to avoid accessing the partial initialized fields */
nxsched_get_tls(tcb)->tl_argv = stackargv;
return OK;
return ret;
}
/****************************************************************************