[Xcb] [PATCH 2/2] Make xcb_disconnect(NULL) safe

Josh Triplett josh at joshtriplett.org
Tue Dec 31 14:00:57 PST 2013


On Tue, Dec 31, 2013 at 03:57:26PM +0100, Uli Schlachter wrote:
> Code can be simplified if the deallocation functions can always be called in
> cleanup code. So if you have some code that does several things that can go
> wrong, one of which is xcb_connect(), after this change, the xcb_connection_t*
> variable can be initialized to NULL and xcb_disconnect() can always be called on
> the connection object.
> 
> References: http://lists.freedesktop.org/archives/xcb/2013-September/008659.html
> 
> Signed-off-by: Uli Schlachter <psychon at znc.in>

Reviewed-by: Josh Triplett <josh at joshtriplett.org>

>  src/xcb.h      | 2 +-
>  src/xcb_conn.c | 3 ++-
>  2 files changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/src/xcb.h b/src/xcb.h
> index e62c985..34585c5 100644
> --- a/src/xcb.h
> +++ b/src/xcb.h
> @@ -482,7 +482,7 @@ xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info);
>   * @param c: The connection.
>   *
>   * Closes the file descriptor and frees all memory associated with the
> - * connection @c c.
> + * connection @c c. If @p c is @c NULL, nothing is done.
>   */
>  void xcb_disconnect(xcb_connection_t *c);
>  
> diff --git a/src/xcb_conn.c b/src/xcb_conn.c
> index 9da11b0..5832e82 100644
> --- a/src/xcb_conn.c
> +++ b/src/xcb_conn.c
> @@ -342,7 +342,8 @@ xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
>  
>  void xcb_disconnect(xcb_connection_t *c)
>  {
> -    if(c == (xcb_connection_t *) &xcb_con_error ||
> +    if(c == NULL ||
> +            c == (xcb_connection_t *) &xcb_con_error ||
>              c == (xcb_connection_t *) &xcb_con_closed_mem_er ||
>              c == (xcb_connection_t *) &xcb_con_closed_parse_er ||
>              c == (xcb_connection_t *) &xcb_con_closed_screen_er)
> -- 
> 1.8.5.2
> 
> _______________________________________________
> Xcb mailing list
> Xcb at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/xcb


More information about the Xcb mailing list