[Xcb] [PATCH:xcb-util] Make sure wm_class name strings are null-terminated

Alan Coopersmith alan.coopersmith at oracle.com
Sun Jun 13 14:38:48 PDT 2010


Previously it worked unless the string happened to be just the right
length that there was no padding at the end of the reply.   This fixes
that case at the cost of always allocating new memory for the strings.

Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 icccm/icccm.c |   15 ++++++++++++++-
 1 files changed, 14 insertions(+), 1 deletions(-)

diff --git a/icccm/icccm.c b/icccm/icccm.c
index e620bde..868d1b1 100644
--- a/icccm/icccm.c
+++ b/icccm/icccm.c
@@ -359,7 +359,19 @@ xcb_get_wm_class_from_reply(xcb_get_wm_class_reply_t *prop,
     return 0;
 
   prop->_reply = reply;
-  prop->instance_name = (char *) xcb_get_property_value(prop->_reply);
+
+  /* Reply will not be null terminated, but since caller won't know the
+     length we have to copy to a null terminated string.
+     If both instance and class are provided, there will be a null byte
+     separating them, otherwise we just point class to the null terminator. */
+  int reply_length = xcb_get_property_value_length(reply);
+  char *value = (char *) xcb_get_property_value(prop->_reply);
+
+  prop->instance_name = malloc(reply_length + 1);
+  if (!prop->instance_name)
+    return 0;
+  memcpy(prop->instance_name, value, reply_length);
+  prop->instance_name[reply_length] = '\0';
 
   int name_len = strlen(prop->instance_name);
   if(name_len == xcb_get_property_value_length(prop->_reply))
@@ -385,6 +397,7 @@ xcb_get_wm_class_reply(xcb_connection_t *c, xcb_get_property_cookie_t cookie,
 void
 xcb_get_wm_class_reply_wipe(xcb_get_wm_class_reply_t *prop)
 {
+  free(prop->instance_name);
   free(prop->_reply);
 }
 
-- 
1.5.6.5



More information about the Xcb mailing list