From 4d15d46473907515d31cd0dc92b3ce86e5c5cb1e Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 26 Nov 2017 08:45:19 +0000 Subject: [PATCH] Memory leak: free conn->retbuf in uxagentc.c. While debugging some new code, I ran valgrind in leak-checking mode and it pointed out a handful of existing memory leaks, which got in the way of spotting any _new_ leaks I might be introducing :-) This was one: in the case where an asynchronous agent query on Unix is aborted, the dynamically allocated buffer holding the response was not freed. --- unix/uxagentc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/unix/uxagentc.c b/unix/uxagentc.c index 51f9a1eb..2c748343 100644 --- a/unix/uxagentc.c +++ b/unix/uxagentc.c @@ -95,6 +95,8 @@ void agent_cancel_query(agent_pending_query *conn) uxsel_del(conn->fd); close(conn->fd); del234(agent_pending_queries, conn); + if (conn->retbuf && conn->retbuf != conn->sizebuf) + sfree(conn->retbuf); sfree(conn); } @@ -114,11 +116,12 @@ static void agent_select_result(int fd, int event) return; /* more data to come */ /* - * We have now completed the agent query. Do the callback, and - * clean up. (Of course we don't free retbuf, since ownership - * of that passes to the callback.) + * We have now completed the agent query. Do the callback. */ conn->callback(conn->callback_ctx, conn->retbuf, conn->retlen); + /* Null out conn->retbuf, since ownership of that buffer has + * passed to the callback. */ + conn->retbuf = NULL; agent_cancel_query(conn); }