From e7ba55789ccf61d22c76662b08648160a742aa88 Mon Sep 17 00:00:00 2001 From: Hans de Goede Date: Sat, 26 Mar 2011 17:12:02 +0100 Subject: [PATCH] udscs: Let the read call back take owner ship of the data This avoids the need to do a memcpy if the read callback wants to keep the data around. This makes the read callback responsible for freeing the buffer. --- udscs.c | 1 - udscs.h | 2 +- vdagent-x11.c | 5 ++++- vdagent-x11.h | 2 +- vdagent.c | 9 ++++++++- vdagentd.c | 3 ++- 6 files changed, 16 insertions(+), 6 deletions(-) diff --git a/udscs.c b/udscs.c index 2ebbadf..fc5e333 100644 --- a/udscs.c +++ b/udscs.c @@ -466,7 +466,6 @@ static void udscs_read_complete(struct udscs_connection **connp) return; } - free(conn->data.buf); conn->header_read = 0; memset(&conn->data, 0, sizeof(conn->data)); } diff --git a/udscs.h b/udscs.h index 2bb9bba..72e58ed 100644 --- a/udscs.h +++ b/udscs.h @@ -42,7 +42,7 @@ typedef void (*udscs_connect_callback)(struct udscs_connection *conn); received. The callback may call udscs_destroy_connection, in which case *connp must be made NULL (which udscs_destroy_connection takes care of) */ typedef void (*udscs_read_callback)(struct udscs_connection **connp, - struct udscs_message_header *header, const uint8_t *data); + struct udscs_message_header *header, uint8_t *data); /* Callback type for udscs_server_for_all_clients. Clients can be disconnected from this callback just like with a read callback. */ typedef int (*udscs_for_all_clients_callback)(struct udscs_connection **connp, diff --git a/vdagent-x11.c b/vdagent-x11.c index 5febe71..fd4d1ec 100644 --- a/vdagent-x11.c +++ b/vdagent-x11.c @@ -905,7 +905,7 @@ void vdagent_x11_clipboard_grab(struct vdagent_x11 *x11, uint32_t *types, } void vdagent_x11_clipboard_data(struct vdagent_x11 *x11, uint32_t type, - const uint8_t *data, uint32_t size) + uint8_t *data, uint32_t size) { Atom prop; XEvent *event; @@ -914,6 +914,7 @@ void vdagent_x11_clipboard_data(struct vdagent_x11 *x11, uint32_t type, if (!x11->selection_request) { fprintf(x11->errfile, "received clipboard data without an outstanding" "selection request, ignoring\n"); + free(data); return; } @@ -924,6 +925,7 @@ void vdagent_x11_clipboard_data(struct vdagent_x11 *x11, uint32_t type, fprintf(x11->errfile, "expecting type %u clipboard data got %u\n", type_from_event, type); vdagent_x11_send_selection_notify(x11, None, 1); + free(data); return; } @@ -936,6 +938,7 @@ void vdagent_x11_clipboard_data(struct vdagent_x11 *x11, uint32_t type, event->xselectionrequest.target, 8, PropModeReplace, data, size); vdagent_x11_send_selection_notify(x11, prop, 1); + free(data); } void vdagent_x11_clipboard_release(struct vdagent_x11 *x11) diff --git a/vdagent-x11.h b/vdagent-x11.h index 55baf82..10f27ee 100644 --- a/vdagent-x11.h +++ b/vdagent-x11.h @@ -41,7 +41,7 @@ void vdagent_x11_clipboard_grab(struct vdagent_x11 *x11, uint32_t *types, uint32_t type_count); void vdagent_x11_clipboard_request(struct vdagent_x11 *x11, uint32_t type); void vdagent_x11_clipboard_data(struct vdagent_x11 *x11, uint32_t type, - const uint8_t *data, uint32_t size); + uint8_t *data, uint32_t size); void vdagent_x11_clipboard_release(struct vdagent_x11 *x11); #endif diff --git a/vdagent.c b/vdagent.c index 233fd2c..7f1bee9 100644 --- a/vdagent.c +++ b/vdagent.c @@ -42,29 +42,36 @@ static FILE *logfile = NULL; static int quit = 0; void daemon_read_complete(struct udscs_connection **connp, - struct udscs_message_header *header, const uint8_t *data) + struct udscs_message_header *header, uint8_t *data) { switch (header->type) { case VDAGENTD_MONITORS_CONFIG: vdagent_x11_set_monitor_config(x11, (VDAgentMonitorsConfig *)data); + free(data); break; case VDAGENTD_CLIPBOARD_REQUEST: vdagent_x11_clipboard_request(x11, header->opaque); + free(data); break; case VDAGENTD_CLIPBOARD_GRAB: vdagent_x11_clipboard_grab(x11, (uint32_t *)data, header->size / sizeof(uint32_t)); + free(data); break; case VDAGENTD_CLIPBOARD_DATA: vdagent_x11_clipboard_data(x11, header->opaque, data, header->size); + /* vdagent_x11_clipboard_data takes ownership of the data (or frees + it immediately) */ break; case VDAGENTD_CLIPBOARD_RELEASE: vdagent_x11_clipboard_release(x11); + free(data); break; default: if (verbose) fprintf(logfile, "Unknown message from vdagentd type: %d\n", header->type); + free(data); } } diff --git a/vdagentd.c b/vdagentd.c index a88bdf5..6365552 100644 --- a/vdagentd.c +++ b/vdagentd.c @@ -466,7 +466,7 @@ void agent_disconnect(struct udscs_connection *conn) } void agent_read_complete(struct udscs_connection **connp, - struct udscs_message_header *header, const uint8_t *data) + struct udscs_message_header *header, uint8_t *data) { struct agent_data *agent_data = udscs_get_user_data(*connp); @@ -497,6 +497,7 @@ void agent_read_complete(struct udscs_connection **connp, fprintf(logfile, "unknown message from vdagent: %u, ignoring\n", header->type); } + free(data); } /* main */ -- 2.11.4.GIT