Patchwork [3/5] cmd: Fix potential memory leak

login
register
mail settings
Submitter Stefan Hajnoczi
Date Nov. 7, 2011, 9:26 a.m.
Message ID <1320658020-4374-4-git-send-email-stefanha@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/124021/
State New
Headers show

Comments

Stefan Hajnoczi - Nov. 7, 2011, 9:26 a.m.
From: Pavel Borzenkov <pavel.borzenkov@gmail.com>

Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 cmd.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

Patch

diff --git a/cmd.c b/cmd.c
index 75415d8..0806e18 100644
--- a/cmd.c
+++ b/cmd.c
@@ -329,16 +329,21 @@  char **breakline(char *input, int *count)
     int c = 0;
     char *p;
     char **rval = calloc(sizeof(char *), 1);
+    char **tmp;
 
     while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
         if (!*p) {
             continue;
         }
         c++;
-        rval = realloc(rval, sizeof(*rval) * (c + 1));
-        if (!rval) {
+        tmp = realloc(rval, sizeof(*rval) * (c + 1));
+        if (!tmp) {
+            free(rval);
+            rval = NULL;
             c = 0;
             break;
+        } else {
+            rval = tmp;
         }
         rval[c - 1] = p;
         rval[c] = NULL;