diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2014-11-13 06:35:20 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2014-11-13 06:35:20 -0600 |
commit | 725f4deb901ede258944bbd9c0cab44ff17bafa8 (patch) | |
tree | 2ec35705ebc99ac459a4c74ce7b71ecedde3163f /nuttx/libc | |
parent | fec3f006aae0918f779c5189105f625d19696ed0 (diff) | |
download | nuttx-725f4deb901ede258944bbd9c0cab44ff17bafa8.tar.gz nuttx-725f4deb901ede258944bbd9c0cab44ff17bafa8.tar.bz2 nuttx-725f4deb901ede258944bbd9c0cab44ff17bafa8.zip |
execl(): Don't allocate or free and argv[] list if there are not arguments
Diffstat (limited to 'nuttx/libc')
-rw-r--r-- | nuttx/libc/unistd/lib_execl.c | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/nuttx/libc/unistd/lib_execl.c b/nuttx/libc/unistd/lib_execl.c index 873bbc429..aa57c4cb5 100644 --- a/nuttx/libc/unistd/lib_execl.c +++ b/nuttx/libc/unistd/lib_execl.c @@ -124,7 +124,7 @@ int execl(FAR const char *path, ...) { - FAR char **argv; + FAR char **argv = (FAR char **)NULL; size_t nargs; va_list ap; int argc; @@ -152,32 +152,39 @@ int execl(FAR const char *path, ...) /* Allocate a temporary argv[] array */ - argv = (FAR char **)malloc((nargs + 1) * sizeof(FAR char *)); - if (argv = (FAR char **)NULL) + if (nargs > 0) { - set_errno(ENOMEM); - return ERROR; - } + argv = (FAR char **)malloc((nargs + 1) * sizeof(FAR char *)); + if (argv = (FAR char **)NULL) + { + set_errno(ENOMEM); + return ERROR; + } - /* Collect the arguments into the argv[] array */ + /* Collect the arguments into the argv[] array */ - va_start(ap, path); - for (argc = 0; argc < nargs; argc++) - { - argv[argc] = va_arg(ap, FAR char *); - } + va_start(ap, path); + for (argc = 0; argc < nargs; argc++) + { + argv[argc] = va_arg(ap, FAR char *); + } - argv[nargs] = NULL; - va_end(ap); + argv[nargs] = NULL; + va_end(ap); + } /* Then let execv() do the real work */ - ret = execv(path, (char * const *)&argv); + ret = execv(path, (FAR char * const *)argv); /* Free the allocated argv[] list */ - free(argv); + if (argv) + { + free(argv); + } + return ret; } -#endif /* CONFIG_LIBC_EXECFUNCS */
\ No newline at end of file +#endif /* CONFIG_LIBC_EXECFUNCS */ |