mirror of
https://github.com/apache/nuttx.git
synced 2026-05-29 04:19:37 +08:00
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:
+76
-64
@@ -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,80 +563,91 @@ 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
|
|
||||||
* 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)
|
|
||||||
{
|
{
|
||||||
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
|
if (ret == OK)
|
||||||
* 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)
|
|
||||||
{
|
{
|
||||||
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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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;
|
argvlen = (argc + 2) * sizeof(FAR char *);
|
||||||
nbytes = strlen(argv[i]) + 1;
|
stackargv = (FAR char **)up_stack_frame(tcb, argvlen + strtablen);
|
||||||
strlcpy(str, argv[i], strtablen);
|
|
||||||
str += nbytes;
|
DEBUGASSERT(stackargv != NULL);
|
||||||
strtablen -= nbytes;
|
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
|
return ret;
|
||||||
* 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
|||||||
Reference in New Issue
Block a user