[fedora-virt] [PATCH libguestfs] avoid leak upon failed realloc

Jim Meyering jim at meyering.net
Fri Jul 3 13:12:49 UTC 2009


Hi again,

No big deal, but might as well fix this...


>From a4040866dd60c92bb2994f8fd9f0d57b62f1842c Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Fri, 3 Jul 2009 12:05:22 +0200
Subject: [PATCH libguestfs] avoid leak upon failed realloc

* daemon/guestfsd.c (commandrv): Free original buffer (rather
than leaking it) if realloc fails.
---
 daemon/guestfsd.c |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c
index 581c82e..87065b9 100644
--- a/daemon/guestfsd.c
+++ b/daemon/guestfsd.c
@@ -572,19 +572,23 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv)
    * trailing \n characters from the error buffer (not from stdout).
    */
   if (stdoutput) {
-    *stdoutput = realloc (*stdoutput, so_size+1);
-    if (*stdoutput == NULL) {
+    void *q = realloc (*stdoutput, so_size+1);
+    if (q == NULL) {
       perror ("realloc");
-      *stdoutput = NULL;
-    } else
+      free (*stdoutput);
+    }
+    *stdoutput = q;
+    if (*stdoutput)
       (*stdoutput)[so_size] = '\0';
   }
   if (stderror) {
-    *stderror = realloc (*stderror, se_size+1);
-    if (*stderror == NULL) {
+    void *q = realloc (*stderror, se_size+1);
+    if (q == NULL) {
       perror ("realloc");
-      *stderror = NULL;
-    } else {
+      free (*stderror);
+    }
+    *stderror = q;
+    if (*stderror) {
       (*stderror)[se_size] = '\0';
       se_size--;
       while (se_size >= 0 && (*stderror)[se_size] == '\n')
--
1.6.3.3.507.gc6b5a




More information about the Fedora-virt mailing list