[Xcb] [PATCH 2/2] xcb_get_setup(): Never return NULL

Uli Schlachter psychon at znc.in
Mon Aug 18 01:38:48 PDT 2014


The documentation doesn't mention it and it's unlikely that a lot of code out
there handles this case correctly. So, instead of returning NULL, let
xcb_get_setup() return a pointer to a static, invalid, all-zero setup
information structure.

Signed-off-by: Uli Schlachter <psychon at znc.in>
---
I tested this against xcb_render_util_query_version(xcb_connect(NULL, NULL))
which crashed with e.g. DISPLAY=:42 before this patch. Should I mention
something like this in the commit message?

 src/xcb_conn.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/xcb_conn.c b/src/xcb_conn.c
index 15f707a..7d09637 100644
--- a/src/xcb_conn.c
+++ b/src/xcb_conn.c
@@ -64,6 +64,26 @@ typedef struct {
     uint16_t length;
 } xcb_setup_generic_t;
 
+static const xcb_setup_t xcb_error_setup = {
+    0,     /* status: failed (but we wouldn't have a xcb_setup_t in this case) */
+    0,     /* pad0 */
+    0, 0,  /* protocol version, should be 11.0, but isn't */
+    0,     /* length, invalid value */
+    0,     /* release_number */
+    0, 0,  /* resource_id_{base,mask} */
+    0,     /* motion_buffer_size */
+    0,     /* vendor_len */
+    0,     /* maximum_request_length */
+    0,     /* roots_len */
+    0,     /* pixmap_formats_len */
+    0,     /* image_byte_order */
+    0,     /* bitmap_format_bit_order */
+    0,     /* bitmap_format_scanline_unit */
+    0,     /* bitmap_format_scanline_pad */
+    0, 0,  /* {min,max}_keycode */
+    { 0, 0, 0, 0 } /* pad1 */
+};
+
 /* Keep this list in sync with is_static_error_conn()! */
 static const int xcb_con_error = XCB_CONN_ERROR;
 static const int xcb_con_closed_mem_er = XCB_CONN_CLOSED_MEM_INSUFFICIENT;
@@ -289,7 +309,7 @@ static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
 const xcb_setup_t *xcb_get_setup(xcb_connection_t *c)
 {
     if(is_static_error_conn(c))
-        return 0;
+        return &xcb_error_setup;
     /* doesn't need locking because it's never written to. */
     return c->setup;
 }
-- 
2.0.1



More information about the Xcb mailing list