From 7847c1f974f1098cfd4feaa200b72f139ddd66a1 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Wed, 16 Jun 2021 02:55:57 +0800 Subject: [PATCH] example/posix_spawn: Ensure argv has filename and NULL terminator Signed-off-by: Xiang Xiao --- examples/elf/elf_main.c | 5 +++-- examples/nxflat/nxflat_main.c | 5 +++-- examples/posix_spawn/spawn_main.c | 27 ++++++++++++++++----------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/examples/elf/elf_main.c b/examples/elf/elf_main.c index 94e3454ff..aa10e01d0 100644 --- a/examples/elf/elf_main.c +++ b/examples/elf/elf_main.c @@ -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"); diff --git a/examples/nxflat/nxflat_main.c b/examples/nxflat/nxflat_main.c index 568aa328d..e951ca0dc 100644 --- a/examples/nxflat/nxflat_main.c +++ b/examples/nxflat/nxflat_main.c @@ -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) { diff --git a/examples/posix_spawn/spawn_main.c b/examples/posix_spawn/spawn_main.c index 4d5e88cbb..0ef77ed79 100644 --- a/examples/posix_spawn/spawn_main.c +++ b/examples/posix_spawn/spawn_main.c @@ -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);