[PATCH 2/2] irmd: Fix argvdup util function

  • From: Dimitri Staessens <dimitri@ouroboros.rocks>
  • To: ouroboros@xxxxxxxxxxxxx
  • Date: Tue, 15 Feb 2022 20:23:50 +0100

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 <dimitri@ouroboros.rocks>
---
 src/irmd/utils.c | 22 ++++++++++++----------
 1 file 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;
 }
-- 
2.35.1


Other related posts:

  • » [PATCH 2/2] irmd: Fix argvdup util function - Dimitri Staessens