[Xcb-commit] xcb/src xcb_util.c,1.9,1.10

Jamey Sharp xcb-commit at lists.freedesktop.org
Mon Dec 12 15:40:49 PST 2005


Update of /cvs/xcb/xcb/src
In directory gabe:/tmp/cvs-serv5221/src

Modified Files:
	xcb_util.c 
Log Message:
Rewrite XCBParseDisplay to handle all error cases correctly.
Thanks to Travis Spencer for pointing out a problem with
the previous sscanf call; turns out that sscanf won't do what
I want anyway, so this version just uses strtoul.


Index: xcb_util.c
===================================================================
RCS file: /cvs/xcb/xcb/src/xcb_util.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- xcb_util.c	8 Dec 2005 05:51:19 -0000	1.9
+++ xcb_util.c	12 Dec 2005 23:40:47 -0000	1.10
@@ -49,31 +49,45 @@
     return ((y + (y >> 3)) & 030707070707) % 077;
 }
 
-int XCBParseDisplay(const char *name, char **host, int *display, int *screen)
+int XCBParseDisplay(const char *name, char **host, int *displayp, int *screenp)
 {
-    char *colon;
-    int dummy_screen;
-    if(!screen)
-        screen = &dummy_screen;
-    *screen = *display = 0;
+    int len, display, screen;
+    char *colon, *dot, *end;
     if(!name || !*name)
         name = getenv("DISPLAY");
     if(!name)
         return 0;
-    *host = malloc(strlen(name) + 1);
-    if(!*host)
-        return 0;
-    strcpy(*host, name);
-    colon = strchr(*host, ':');
+    colon = strrchr(name, ':');
     if(!colon)
-    {
-        free(*host);
-        *host = 0;
         return 0;
-    }
-    *colon = '\0';
+    len = colon - name;
     ++colon;
-    return sscanf(colon, "%d.%d", display, screen);
+    display = strtoul(colon, &dot, 10);
+    if(dot == colon)
+        return 0;
+    if(*dot == '\0')
+        screen = 0;
+    else
+    {
+        if(*dot != '.')
+            return 0;
+        ++dot;
+        screen = strtoul(dot, &end, 10);
+        if(end == dot || *end != '\0')
+            return 0;
+    }
+    /* At this point, the display string is fully parsed and valid, but
+     * the caller's memory is untouched. */
+
+    *host = malloc(len + 1);
+    if(!*host)
+        return 0;
+    memcpy(*host, name, len);
+    (*host)[len] = '\0';
+    *displayp = display;
+    if(screenp)
+        *screenp = screen;
+    return 1;
 }
 
 int XCBOpen(const char *host, const int display)



More information about the xcb-commit mailing list