[Xcb] [PATCH] auth: use snprintf() return value

Julien Danjou julien at danjou.info
Tue Apr 7 04:29:17 PDT 2009


That save us from a strlen().

Signed-off-by: Julien Danjou <julien at danjou.info>
---

After rereading my patch, it seemed a bit too optimistic.
This one should be more safe.

 src/xcb_auth.c |    9 +++++++--
 src/xcbint.h   |    4 ++++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/xcb_auth.c b/src/xcb_auth.c
index 1645ce8..c449379 100644
--- a/src/xcb_auth.c
+++ b/src/xcb_auth.c
@@ -94,6 +94,7 @@ static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen,
     unsigned short family;
     char hostnamebuf[256];   /* big enough for max hostname */
     char dispbuf[40];   /* big enough to hold more than 2^64 base 10 */
+    int dispbuflen;
 
     family = FamilyLocal; /* 256 */
     switch(sockname->sa_family)
@@ -124,7 +125,11 @@ static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen,
         return 0;   /* cannot authenticate this family */
     }
 
-    snprintf(dispbuf, sizeof(dispbuf), "%d", display);
+    dispbuflen = snprintf(dispbuf, sizeof(dispbuf), "%d", display);
+    if(dispbuflen < 0)
+        return 0;
+    /* snprintf may have truncate our text */
+    dispbuflen = MIN(dispbuflen, sizeof(dispbuf) - 1);
 
     if (family == FamilyLocal) {
         if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
@@ -135,7 +140,7 @@ static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen,
 
     return XauGetBestAuthByAddr (family,
                                  (unsigned short) addrlen, addr,
-                                 (unsigned short) strlen(dispbuf), dispbuf,
+                                 (unsigned short) dispbuflen, dispbuf,
                                  N_AUTH_PROTOS, authnames, authnamelens);
 }
 
diff --git a/src/xcbint.h b/src/xcbint.h
index dac0a61..154cca0 100644
--- a/src/xcbint.h
+++ b/src/xcbint.h
@@ -60,6 +60,10 @@ enum lazy_reply_tag
 #define offsetof(type,member) ((size_t) &((type *)0)->member)
 #endif
 
+#ifndef MIN
+#define MIN(x,y) ((x) < (y) ? (x) : (y))
+#endif
+
 #define container_of(pointer,type,member) ((type *)(((char *)(pointer)) - offsetof(type, member)))
 
 /* xcb_list.c */
-- 
1.6.2.2



More information about the Xcb mailing list