Patchwork cmd: Fix potential memory leak

login
register
mail settings
Submitter Pavel Borzenkov
Date Oct. 28, 2011, 9:50 p.m.
Message ID <1319838633-25493-1-git-send-email-pavel.borzenkov@gmail.com>
Download mbox | patch
Permalink /patch/122500/
State New
Headers show

Comments

Pavel Borzenkov - Oct. 28, 2011, 9:50 p.m.
Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
---
Note, that almost all code in the file violates CODING STYLE. The changed lines
are written wrt CODING STYLE (like commits 7d7d975c and c32d766a).
---
 cmd.c |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)

Patch

diff --git a/cmd.c b/cmd.c
index abcf206..e1ae5a0 100644
--- a/cmd.c
+++ b/cmd.c
@@ -334,16 +334,21 @@  breakline(
 	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;
 	}