From 12b084b62109e6dc43792fc4af5accb21b469d05 Mon Sep 17 00:00:00 2001 From: oni-link Date: Thu, 12 Feb 2015 14:07:05 +0100 Subject: [PATCH] Fix a memory leak for WBuffers used in channel_write(). channel_write() uses a ref-counted buffer for writing. This buffer should be released if it was used in "refcount" channel_write() calls. But calling channel_write() on a closed channel would return early and not decrease the refcount of the used buffer. --- src/nvim/msgpack_rpc/channel.c | 1 + src/nvim/os/wstream.c | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/nvim/msgpack_rpc/channel.c b/src/nvim/msgpack_rpc/channel.c index ab67500fdc..35549ce042 100644 --- a/src/nvim/msgpack_rpc/channel.c +++ b/src/nvim/msgpack_rpc/channel.c @@ -495,6 +495,7 @@ static bool channel_write(Channel *channel, WBuffer *buffer) bool success; if (channel->closed) { + wstream_release_wbuffer(buffer); return false; } diff --git a/src/nvim/os/wstream.c b/src/nvim/os/wstream.c index 90d4ebeec8..13c6c0429f 100644 --- a/src/nvim/os/wstream.c +++ b/src/nvim/os/wstream.c @@ -181,7 +181,7 @@ bool wstream_write(WStream *wstream, WBuffer *buffer) return true; err: - release_wbuffer(buffer); + wstream_release_wbuffer(buffer); return false; } @@ -217,7 +217,7 @@ static void write_cb(uv_write_t *req, int status) data->wstream->curmem -= data->buffer->size; - release_wbuffer(data->buffer); + wstream_release_wbuffer(data->buffer); if (data->wstream->cb) { data->wstream->cb(data->wstream, @@ -239,7 +239,7 @@ static void write_cb(uv_write_t *req, int status) kmp_free(WRequestPool, wrequest_pool, data); } -static void release_wbuffer(WBuffer *buffer) +void wstream_release_wbuffer(WBuffer *buffer) { if (!--buffer->refcount) { if (buffer->cb) {