[PATCH xserver 4/8] dmx: Clean up some argument parsing code

Adam Jackson ajax at redhat.com
Thu Apr 5 17:13:57 UTC 2018


This threw:

../hw/dmx/input/dmxarg.c: In function ‘dmxArgParse’:
../hw/dmx/input/dmxarg.c:128:5: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-overflow=]
     strncpy(tmp, string, len);
     ^~~~~~~~~~~~~~~~~~~~~~~~~
../hw/dmx/input/dmxarg.c:126:11: note: length computed here
     len = strlen(string) + 2;
           ^~~~~~~~~~~~~~

This code predates xstrtokenize, but that's no excuse.

Signed-off-by: Adam Jackson <ajax at redhat.com>
---
 hw/dmx/input/dmxarg.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)

diff --git a/hw/dmx/input/dmxarg.c b/hw/dmx/input/dmxarg.c
index 6c21ae959a..582ed3faa6 100644
--- a/hw/dmx/input/dmxarg.c
+++ b/hw/dmx/input/dmxarg.c
@@ -114,30 +114,17 @@ dmxArgC(dmxArg a)
 dmxArg
 dmxArgParse(const char *string)
 {
-    char *tmp;
-    char *start, *pt;
+    int i = 0;
     dmxArg a = dmxArgCreate();
-    int done;
-    int len;
 
     if (!string)
         return a;
 
-    len = strlen(string) + 2;
-    tmp = malloc(len);
-    strncpy(tmp, string, len);
+    a->argv = (const char **)xstrtokenize(string, ",");
+    if (a->argv)
+        for (i = 0; a->argv[i] != NULL; i++);
+    a->argc = i;
 
-    for (start = pt = tmp, done = 0; !done && *pt; start = ++pt) {
-        for (; *pt && *pt != ','; pt++);
-        if (!*pt)
-            done = 1;
-        *pt = '\0';
-        dmxArgAdd(a, start);
-    }
-    if (!done)
-        dmxArgAdd(a, "");       /* Final comma */
-
-    free(tmp);
     return a;
 }
 
-- 
2.16.2



More information about the xorg-devel mailing list