example/posix_spawn: Ensure argv has filename and NULL terminator

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2021-06-16 02:55:57 +08:00 committed by patacongo
parent 340c932e94
commit 7847c1f974
3 changed files with 22 additions and 15 deletions

View File

@ -206,7 +206,7 @@ int main(int argc, FAR char *argv[])
#ifdef CONFIG_EXAMPLES_ELF_ROMFS
struct boardioc_romdisk_s desc;
#endif
FAR char *args[1];
FAR char *args[2];
int ret;
int i;
@ -357,7 +357,8 @@ int main(int argc, FAR char *argv[])
* table information is available within the OS.
*/
args[0] = NULL;
args[0] = (FAR char *)dirlist[i];
args[1] = NULL;
ret = exec(filename, args, g_elf_exports, g_elf_nexports);
mm_update(&g_mmstep, "after exec");

View File

@ -135,7 +135,7 @@ static inline void testheader(FAR const char *progname)
int main(int argc, FAR char *argv[])
{
FAR char *args[1];
FAR char *args[2];
int ret;
int i;
struct boardioc_romdisk_s desc;
@ -211,7 +211,8 @@ int main(int argc, FAR char *argv[])
* table information is available within the OS.
*/
args[0] = NULL;
args[0] = (FAR char *)dirlist[i];
args[1] = NULL;
ret = exec(filename, args, g_nxflat_exports, g_nxflat_nexports);
if (ret < 0)
{

View File

@ -117,16 +117,20 @@ static unsigned int g_mmstep; /* Memory Usage at beginning of test step */
static const char delimiter[] =
"**************************************"
"**************************************";
static const char g_redirect[] = "redirect";
static const char g_data[] = "testdata.txt";
static char fullpath[128];
static char * const g_argv[5] =
static char * const g_hello_argv[5] =
{
"hello", "Argument 1", "Argument 2", "Argument 3", NULL
};
static char * const g_redirect_argv[2] =
{
"redirect", NULL
};
/****************************************************************************
* Symbols from Auto-Generated Code
****************************************************************************/
@ -270,7 +274,7 @@ int main(int argc, FAR char *argv[])
* this program from the others.
*/
testheader(g_argv[0]);
testheader(g_hello_argv[0]);
/* Initialize the attributes file actions structure */
@ -299,9 +303,9 @@ int main(int argc, FAR char *argv[])
*/
#ifdef CONFIG_LIB_ENVPATH
filepath = g_argv[0];
filepath = g_hello_argv[0];
#else
snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_argv[0]);
snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_hello_argv[0]);
filepath = fullpath;
#endif
@ -309,8 +313,8 @@ int main(int argc, FAR char *argv[])
mm_update(&g_mmstep, "before posix_spawn");
ret = posix_spawn(&pid, filepath, &file_actions, &attr, NULL,
(FAR char * const *)&g_argv);
ret = posix_spawn(&pid, filepath, &file_actions,
&attr, g_hello_argv, NULL);
if (ret != 0)
{
errmsg("ERROR: posix_spawn failed: %d\n", ret);
@ -347,7 +351,7 @@ int main(int argc, FAR char *argv[])
* this program from the others.
*/
testheader(g_redirect);
testheader(g_redirect_argv[0]);
/* Initialize the attributes file actions structure */
@ -398,9 +402,9 @@ int main(int argc, FAR char *argv[])
*/
#ifdef CONFIG_LIB_ENVPATH
filepath = g_redirect;
filepath = g_redirect_argv[0];
#else
snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_redirect);
snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_redirect_argv[0]);
filepath = fullpath;
#endif
@ -408,7 +412,8 @@ int main(int argc, FAR char *argv[])
mm_update(&g_mmstep, "before posix_spawn");
ret = posix_spawn(&pid, filepath, &file_actions, &attr, NULL, NULL);
ret = posix_spawn(&pid, filepath, &file_actions,
&attr, g_redirect_argv, NULL);
if (ret != 0)
{
errmsg("ERROR: posix_spawn failed: %d\n", ret);