diff options
author | Dimitri Staessens <[email protected]> | 2022-02-15 20:23:50 +0100 |
---|---|---|
committer | Sander Vrijders <[email protected]> | 2022-02-17 08:28:53 +0100 |
commit | 4aae321f11e3087d4e4bf65f945e5800f1d4f2c5 (patch) | |
tree | 8adb8cac8ed6a397e304a343a6b962129647317f | |
parent | 6d87e29bef2d2cd3c40e49b9120fae5148030381 (diff) | |
download | ouroboros-4aae321f11e3087d4e4bf65f945e5800f1d4f2c5.tar.gz ouroboros-4aae321f11e3087d4e4bf65f945e5800f1d4f2c5.zip |
irmd: Fix argvdup util function
The argvdup function didn't handle the case where argc is 0.
Small refactor that also handles this case correctly.
Signed-off-by: Dimitri Staessens <[email protected]>
Signed-off-by: Sander Vrijders <[email protected]>
-rw-r--r-- | src/irmd/utils.c | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/src/irmd/utils.c b/src/irmd/utils.c index 976dcfa2..fade2bee 100644 --- a/src/irmd/utils.c +++ b/src/irmd/utils.c @@ -39,9 +39,9 @@ void argvfree(char ** argv) char ** argvdup(char ** argv) { - int argc = 0; + int argc = 0; char ** argv_dup = argv; - int i; + int i; if (argv == NULL) return NULL; @@ -49,16 +49,18 @@ char ** argvdup(char ** argv) while (*(argv_dup++) != NULL) argc++; - if (argc != 0) { - argv_dup = malloc((argc + 1) * sizeof(*argv_dup)); - for (i = 0; i < argc; ++i) { - argv_dup[i] = strdup(argv[i]); - if (argv_dup[i] == NULL) { - argvfree(argv_dup); - return NULL; - } + argv_dup = malloc((argc + 1) * sizeof(*argv_dup)); + if (argv_dup == NULL) + return NULL; + + for (i = 0; i < argc; ++i) { + argv_dup[i] = strdup(argv[i]); + if (argv_dup[i] == NULL) { + argvfree(argv_dup); + return NULL; } } + argv_dup[argc] = NULL; return argv_dup; } |