fix tiny memory leak in read_packet (leak only happens when malloc returns NULL so...
[free-sw/xcb/libxcb] / src / xcb_in.c
index 41764df..f613772 100644 (file)
@@ -39,6 +39,7 @@
 
 #define XCB_ERROR 0
 #define XCB_REPLY 1
+#define XCB_XGE_EVENT 35
 
 struct event_list {
     xcb_generic_event_t *event;
@@ -77,6 +78,7 @@ static int read_packet(xcb_connection_t *c)
 {
     xcb_generic_reply_t genrep;
     int length = 32;
+    int eventlength = 0; /* length after first 32 bytes for GenericEvents */
     void *buf;
     pending_reply *pend = 0;
     struct event_list *event;
@@ -108,8 +110,6 @@ static int read_packet(xcb_connection_t *c)
             }
             c->in.request_completed = c->in.request_read - 1;
         }
-        if(genrep.response_type == XCB_ERROR)
-            c->in.request_completed = c->in.request_read;
 
         while(c->in.pending_replies && 
              XCB_SEQUENCE_COMPARE (c->in.pending_replies->request, <=, c->in.request_completed))
@@ -120,6 +120,9 @@ static int read_packet(xcb_connection_t *c)
                 c->in.pending_replies_tail = &c->in.pending_replies;
             free(oldpend);
         }
+
+        if(genrep.response_type == XCB_ERROR)
+            c->in.request_completed = c->in.request_read;
     }
 
     if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
@@ -140,17 +143,36 @@ static int read_packet(xcb_connection_t *c)
         length += genrep.length * 4;
     }
 
-    buf = malloc(length + (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
+    /* XGE events may have sizes > 32 */
+    if (genrep.response_type == XCB_XGE_EVENT)
+    {
+        eventlength = ((xcb_ge_event_t*)&genrep)->length * 4;
+    }
+
+    buf = malloc(length + eventlength +
+            (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
     if(!buf)
     {
         _xcb_conn_shutdown(c);
         return 0;
     }
+
     if(_xcb_in_read_block(c, buf, length) <= 0)
     {
         free(buf);
         return 0;
     }
+
+    /* pull in XGE event data if available, append after event struct */
+    if (eventlength)
+    {
+        if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
+        {
+            free(buf);
+            return 0;
+        }
+    }
+
     if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
     {
         free(buf);
@@ -169,6 +191,7 @@ static int read_packet(xcb_connection_t *c)
         if(!cur)
         {
             _xcb_conn_shutdown(c);
+            free(buf);
             return 0;
         }
         cur->reply = buf;
@@ -230,7 +253,7 @@ static void free_reply_list(struct reply_list *head)
     }
 }
 
-static int read_block(const int fd, void *buf, const size_t len)
+static int read_block(const int fd, void *buf, const ssize_t len)
 {
     int done = 0;
     while(done < len)
@@ -395,33 +418,19 @@ xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
     return ret;
 }
 
-xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c, int *error)
+xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
 {
+    xcb_generic_event_t *ret = 0;
     if(!c->has_error)
     {
-        xcb_generic_event_t *ret = 0;
-        int success;
         _xcb_lock_io(c);
         /* FIXME: follow X meets Z architecture changes. */
-        success = _xcb_in_read(c);
-        if(success)
+        ret = get_event(c);
+        if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
             ret = get_event(c);
         _xcb_unlock_io(c);
-        if(success)
-        {
-            if(error)
-                *error = 0;
-            return ret;
-        }
     }
-    if(error)
-        *error = -1;
-    else
-    {
-        fprintf(stderr, "xcb_poll_for_event: I/O error occured, but no handler provided.\n");
-        abort();
-    }
-    return 0;
+    return ret;
 }
 
 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)