When converting from a single transaction to a linked list, I forgot to free the storage for each member of the list. Reported-by: Richard W.M. Jones <rjones@redhat.com> Fixes: 7f5bb9bf13f041ea7702bda557d9dd668bc3423a Signed-off-by: Eric Blake <eblake@redhat.com> --- plugins/nbd/nbd.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/nbd/nbd.c b/plugins/nbd/nbd.c index b844bf5..e79042c 100644 --- a/plugins/nbd/nbd.c +++ b/plugins/nbd/nbd.c @@ -311,6 +311,8 @@ nbd_reply_raw (struct handle *h, int *fd) struct reply rep; struct transaction **ptr; struct transaction *trans; + void *buf; + uint32_t count; *fd = -1; if (read_full (h->fd, &rep, sizeof rep) < 0) @@ -334,9 +336,12 @@ nbd_reply_raw (struct handle *h, int *fd) } *fd = trans->u.fds[1]; + buf = trans->buf; + count = trans->count; + free (trans); switch (be32toh (rep.error)) { case NBD_SUCCESS: - if (trans->buf && read_full (h->fd, trans->buf, trans->count) < 0) + if (buf && read_full (h->fd, buf, count) < 0) return nbd_mark_dead (h); return 0; case NBD_EPERM: @@ -399,6 +404,7 @@ nbd_reader (void *handle) abort (); } close (trans->u.fds[1]); + free (trans); } return NULL; } -- 2.14.3
Richard W.M. Jones
2017-Dec-02 22:08 UTC
Re: [Libguestfs] [nbdkit PATCH v2] nbd: Fix memory leak
On Sat, Dec 02, 2017 at 03:07:09PM -0600, Eric Blake wrote:> When converting from a single transaction to a linked list, I > forgot to free the storage for each member of the list.Thanks Eric. This fixes the valgrind problem for me and I have pushed it upstream. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Apparently Analagous Threads
- [nbdkit PATCH 7/7] nbd: Implement structured replies
- [nbdkit PATCH v2 2/2] nbd: Split reading into separate thread
- [nbdkit PATCH] nbd: Drop nbd-standalone fallback
- [nbdkit PATCH v2 1/2] nbd: Add new nbd forwarding plugin
- [nbdkit PATCH] nbd: Rewrite thread passing to use semaphore rather than pipe