libctdb: fix io_elem resource leak on realloc failure.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 18 Jun 2010 06:18:48 +0000 (15:48 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 18 Jun 2010 06:18:48 +0000 (15:48 +0930)
Found by nfsim.

I knew about this, but as we stop when it happens anyway I didn't fix
it.  But it bugs nfsim, so fix it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
libctdb/io_elem.c

index 1a176988d1be4b155ed0c0bfc9ccae0d48ac6d50..e00ddda05667b98669f9300ba81456d9523c6624 100644 (file)
@@ -110,11 +110,13 @@ int read_io_elem(int fd, struct io_elem *io)
                /* Finished.  But maybe this was just header? */
                if (io->len == sizeof(*hdr) && hdr->length > io->len) {
                        int reret;
+                       void *newdata;
                        /* Enlarge and re-read. */
                        io->len = hdr->length;
-                       io->data = realloc(io->data, io->len);
-                       if (!io->data)
+                       newdata = realloc(io->data, io->len);
+                       if (!newdata)
                                return -1;
+                       io->data = newdata;
                        /* Try reading again immediately. */
                        reret = read_io_elem(fd, io);
                        if (reret >= 0)