binfmt: Let binfmt_copyargv return error code

so the caller can distinguish the empty argument and out of memory quickly

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2022-10-17 23:24:34 +08:00 committed by Masayuki Ishikawa
parent 774648de0f
commit 9f4bb7da97
3 changed files with 30 additions and 41 deletions

View File

@ -95,7 +95,11 @@ int binfmt_dumpmodule(FAR const struct binary_s *bin);
* *
****************************************************************************/ ****************************************************************************/
FAR char * const *binfmt_copyargv(FAR char * const *argv); #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
int binfmt_copyargv(FAR char * const **copy, FAR char * const *argv);
#else
# define binfmt_copyargv(copy, argv) (*(copy) = (argv), 0)
#endif
/**************************************************************************** /****************************************************************************
* Name: binfmt_freeargv * Name: binfmt_freeargv
@ -136,9 +140,9 @@ void binfmt_freeargv(FAR char * const *argv);
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_DISABLE_ENVIRON #ifndef CONFIG_DISABLE_ENVIRON
# define binfmt_copyenv(envp) binfmt_copyargv(envp) # define binfmt_copyenv(copy, envp) binfmt_copyargv(copy, envp)
#else #else
# define binfmt_copyenv(envp) (envp) # define binfmt_copyenv(copy, envp) (*(copy) = (envp), 0)
#endif #endif
/**************************************************************************** /****************************************************************************

View File

@ -33,7 +33,7 @@
#include "binfmt.h" #include "binfmt.h"
#ifndef CONFIG_BINFMT_DISABLE #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) && !defined(CONFIG_BINFMT_DISABLE)
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions
@ -66,30 +66,26 @@
* *
****************************************************************************/ ****************************************************************************/
FAR char * const *binfmt_copyargv(FAR char * const *argv) int binfmt_copyargv(FAR char * const **copy, FAR char * const *argv)
{ {
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
FAR char **argvbuf = NULL; FAR char **argvbuf = NULL;
FAR char *ptr; FAR char *ptr;
size_t argvsize; size_t argvsize;
size_t argsize; size_t argsize = 0;
int nargs; int nargs = 0;
int i; int i;
/* Get the number of arguments and the size of the argument list */ /* Get the number of arguments and the size of the argument list */
if (argv) if (argv)
{ {
argsize = 0;
nargs = 0;
for (i = 0; argv[i]; i++) for (i = 0; argv[i]; i++)
{ {
/* Increment the size of the allocation with the size of the next /* Increment the size of the allocation with the size of the next
* string * string
*/ */
argsize += (strlen(argv[i]) + 1); argsize += strlen(argv[i]) + 1;
nargs++; nargs++;
/* This is a sanity check to prevent running away with an /* This is a sanity check to prevent running away with an
@ -100,13 +96,12 @@ FAR char * const *binfmt_copyargv(FAR char * const *argv)
if (nargs > MAX_EXEC_ARGS) if (nargs > MAX_EXEC_ARGS)
{ {
berr("ERROR: Too many arguments: %lu\n", berr("ERROR: Too many arguments: %zu\n", argsize);
(unsigned long)argvsize); return -E2BIG;
return NULL;
} }
} }
binfo("args=%d argsize=%lu\n", nargs, (unsigned long)argsize); binfo("args=%d argsize=%zu\n", nargs, argsize);
/* Allocate the argv array and an argument buffer */ /* Allocate the argv array and an argument buffer */
@ -117,7 +112,7 @@ FAR char * const *binfmt_copyargv(FAR char * const *argv)
if (!ptr) if (!ptr)
{ {
berr("ERROR: Failed to allocate the argument buffer\n"); berr("ERROR: Failed to allocate the argument buffer\n");
return NULL; return -ENOMEM;
} }
/* Copy the argv list */ /* Copy the argv list */
@ -138,13 +133,8 @@ FAR char * const *binfmt_copyargv(FAR char * const *argv)
} }
} }
return (FAR char * const *)argvbuf; *copy = argvbuf;
return OK;
#else
/* Just return the caller's argv pointer */
return argv;
#endif
} }
/**************************************************************************** /****************************************************************************
@ -161,7 +151,6 @@ FAR char * const *binfmt_copyargv(FAR char * const *argv)
* *
****************************************************************************/ ****************************************************************************/
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
void binfmt_freeargv(FAR char * const *argv) void binfmt_freeargv(FAR char * const *argv)
{ {
/* Is there an allocated argument buffer */ /* Is there an allocated argument buffer */
@ -173,6 +162,5 @@ void binfmt_freeargv(FAR char * const *argv)
kmm_free((FAR char **)argv); kmm_free((FAR char **)argv);
} }
} }
#endif
#endif /* !CONFIG_BINFMT_DISABLE */ #endif

View File

@ -142,26 +142,23 @@ int exec_module(FAR const struct binary_s *binp,
return -ENOMEM; return -ENOMEM;
} }
if (argv) ret = binfmt_copyargv(&argv, argv);
if (ret < 0)
{ {
argv = binfmt_copyargv(argv);
if (!argv)
{
ret = -ENOMEM;
goto errout_with_tcb; goto errout_with_tcb;
} }
}
/* Make a copy of the environment here */ /* Make a copy of the environment here */
if (envp || (envp = environ)) if (envp == NULL)
{ {
envp = binfmt_copyenv(envp); envp = environ;
if (!envp)
{
ret = -ENOMEM;
goto errout_with_args;
} }
ret = binfmt_copyenv(&envp, envp);
if (ret < 0)
{
goto errout_with_args;
} }
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)