Patchwork Fix a few memory leaks from early returns

login
register
mail settings
Submitter jschopp@austin.ibm.com
Date July 20, 2010, 7:04 p.m.
Message ID <1279652657-6466-1-git-send-email-jschopp@austin.ibm.com>
Download mbox | patch
Permalink /patch/59356/
State New
Headers show

Comments

jschopp@austin.ibm.com - July 20, 2010, 7:04 p.m.
This is a simple cleanup patch that catches a few places where a function returns early but doesn't free recently malloced memory, resulting in a memory leak.  

Signed-off-by: Joel Schopp <jschopp@austin.ibm.com>
---
 linux-user/elfload.c |    8 ++++++--
 qemu-io.c            |    8 ++++++--
 qemu-sockets.c       |    1 +
 3 files changed, 13 insertions(+), 4 deletions(-)

Patch

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index accb44d..916b733 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -1408,12 +1408,16 @@  static void load_symbols(struct elfhdr *hdr, int fd)
     if (!syms)
         return;
     s->disas_strtab = strings = malloc(strtab.sh_size);
-    if (!s->disas_strtab)
+    if (!s->disas_strtab) {
+        free(syms);
         return;
+    }
 
     lseek(fd, symtab.sh_offset, SEEK_SET);
-    if (read(fd, syms, symtab.sh_size) != symtab.sh_size)
+    if (read(fd, syms, symtab.sh_size) != symtab.sh_size) {
+        free(syms);
         return;
+    }
 
     nsyms = symtab.sh_size / sizeof(struct elf_sym);
 
diff --git a/qemu-io.c b/qemu-io.c
index 7c6120b..9adda4c 100644
--- a/qemu-io.c
+++ b/qemu-io.c
@@ -1131,8 +1131,10 @@  aio_read_f(int argc, char **argv)
 		case 'P':
 			ctx->Pflag = 1;
 			ctx->pattern = parse_pattern(optarg);
-			if (ctx->pattern < 0)
+			if (ctx->pattern < 0) {
+				free(ctx);
 				return 0;
+			}
 			break;
 		case 'q':
 			ctx->qflag = 1;
@@ -1232,8 +1234,10 @@  aio_write_f(int argc, char **argv)
 			break;
 		case 'P':
 			pattern = parse_pattern(optarg);
-			if (pattern < 0)
+			if (pattern < 0) {
+				free(ctx);
 				return 0;
+			}
 			break;
 		default:
 			free(ctx);
diff --git a/qemu-sockets.c b/qemu-sockets.c
index c526324..4d2bd3c 100644
--- a/qemu-sockets.c
+++ b/qemu-sockets.c
@@ -344,6 +344,7 @@  int inet_dgram_opts(QemuOpts *opts)
     if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
         fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
                 gai_strerror(rc));
+        freeaddrinfo(peer);
         return -1;
     }
     if (sockets_debug) {