Skip to contentSkip to conversation

Plug a few trivial resource leaks

PATCH Applied Markus Armbruster 2 participants 7 messages Nov 11, 2011

lore
Share & tools
lore
#0 lore raw

cover Plug a few trivial resource leaks

As usual, shit happens on error paths.

Markus Armbruster (5):
qemu-char: Plug memory leak on qemu_chr_open_pty() error path
ui: Plug memory leaks on parse_keyboard_layout() error path
qemu-sockets: Plug fd leak on unix_connect_opts() error path
posix-aio-compat: Plug memory leak on paio_init() error path
os-posix: Plug fd leak in qemu_create_pidfile()

 os-posix.c         |    3 +++
 posix-aio-compat.c |    1 +
 qemu-char.c        |   19 ++++++++++---------
 qemu-sockets.c     |    1 +
 ui/keymaps.c       |   12 +++++++-----
 5 files changed, 22 insertions(+), 14 deletions(-)
Signature · 1 line
1.7.6.4
#1 lore raw

PATCH 1/5 qemu-char: Plug memory leak on qemu_chr_open_pty() error path

Spotted by Coverity.

---
 qemu-char.c |   19 ++++++++++---------
 1 files changed, 10 insertions(+), 9 deletions(-)
qemu-char.c +10 -9
index 9fd94d1..b562bf8 100644
@@ -985,7 +985,7 @@ static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr)
     CharDriverState *chr;
     PtyCharDriver *s;
     struct termios tty;
-    int slave_fd, len;
+    int master_fd, slave_fd, len;
 #if defined(__OpenBSD__) || defined(__DragonFly__)
     char pty_name[PATH_MAX];
 #define q_ptsname(x) pty_name
@@ -994,10 +994,7 @@ static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr)
 #define q_ptsname(x) ptsname(x)
 #endif
 
-    chr = g_malloc0(sizeof(CharDriverState));
-    s = g_malloc0(sizeof(PtyCharDriver));
-
-    if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
+    if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
         return -errno;
     }
 
@@ -1007,17 +1004,21 @@ static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr)
     tcsetattr(slave_fd, TCSAFLUSH, &tty);
     close(slave_fd);
 
-    len = strlen(q_ptsname(s->fd)) + 5;
+    chr = g_malloc0(sizeof(CharDriverState));
+
+    len = strlen(q_ptsname(master_fd)) + 5;
     chr->filename = g_malloc(len);
-    snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
-    qemu_opt_set(opts, "path", q_ptsname(s->fd));
-    fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
+    snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
+    qemu_opt_set(opts, "path", q_ptsname(master_fd));
+    fprintf(stderr, "char device redirected to %s\n", q_ptsname(master_fd));
 
+    s = g_malloc0(sizeof(PtyCharDriver));
     chr->opaque = s;
     chr->chr_write = pty_chr_write;
     chr->chr_update_read_handler = pty_chr_update_read_handler;
     chr->chr_close = pty_chr_close;
 
+    s->fd = master_fd;
     s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
 
     *_chr = chr;
Signature · 1 line
1.7.6.4
#2 lore raw

PATCH 2/5 ui: Plug memory leaks on parse_keyboard_layout() error path

In reply to Markus Armbruster

Spotted by Coverity.

---
 ui/keymaps.c |   12 +++++++-----
 1 files changed, 7 insertions(+), 5 deletions(-)
ui/keymaps.c +7 -5
index f54a114..f55a2aa 100644
@@ -92,15 +92,17 @@ static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
     int len;
 
     filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
-
-    if (!k)
-	k = g_malloc0(sizeof(kbd_layout_t));
-    if (!(filename && (f = fopen(filename, "r")))) {
+    f = filename ? fopen(filename, "r") : NULL;
+    g_free(filename);
+    if (!f) {
 	fprintf(stderr,
 		"Could not read keymap file: '%s'\n", language);
 	return NULL;
     }
-    g_free(filename);
+
+    if (!k)
+	k = g_malloc0(sizeof(kbd_layout_t));
+
     for(;;) {
 	if (fgets(line, 1024, f) == NULL)
             break;
Signature · 1 line
1.7.6.4
#3 lore raw

PATCH 3/5 qemu-sockets: Plug fd leak on unix_connect_opts() error path

In reply to Markus Armbruster

Spotted by Coverity.

---
 qemu-sockets.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
qemu-sockets.c +1 -0
index 183a9cb..61b2247 100644
@@ -572,6 +572,7 @@ int unix_connect_opts(QemuOpts *opts)
     snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
     if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
         fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
+        close(sock);
 	return -1;
     }
 
Signature · 1 line
1.7.6.4
#4 lore raw

PATCH 4/5 posix-aio-compat: Plug memory leak on paio_init() error path

In reply to Markus Armbruster

Spotted by Coverity.

---
 posix-aio-compat.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
posix-aio-compat.c +1 -0
index d3c1174..0c0035c 100644
@@ -667,6 +667,7 @@ int paio_init(void)
     s->first_aio = NULL;
     if (qemu_pipe(fds) == -1) {
         fprintf(stderr, "failed to create pipe\n");
+        g_free(s);
         return -1;
     }
 
Signature · 1 line
1.7.6.4
#5 lore raw

PATCH 5/5 os-posix: Plug fd leak in qemu_create_pidfile()

In reply to Markus Armbruster

Spotted by Coverity.

---
 os-posix.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
os-posix.c +3 -0
index dbf3b24..dc4a6bb 100644
@@ -372,13 +372,16 @@ int qemu_create_pidfile(const char *filename)
         return -1;
     }
     if (lockf(fd, F_TLOCK, 0) == -1) {
+        close(fd);
         return -1;
     }
     len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid());
     if (write(fd, buffer, len) != len) {
+        close(fd);
         return -1;
     }
 
+    close(fd);
     return 0;
 }
 
Signature · 1 line
1.7.6.4
#6 lore raw
In reply to Markus Armbruster
Quoted 1 line · Markus Armbruster

As usual, shit happens on error paths.

Applied all. Thanks.

Regards,

Anthony Liguori

Quoted 15 lines

Markus Armbruster (5):
qemu-char: Plug memory leak on qemu_chr_open_pty() error path
ui: Plug memory leaks on parse_keyboard_layout() error path
qemu-sockets: Plug fd leak on unix_connect_opts() error path
posix-aio-compat: Plug memory leak on paio_init() error path
os-posix: Plug fd leak in qemu_create_pidfile()

  os-posix.c         |    3 +++
  posix-aio-compat.c |    1 +
  qemu-char.c        |   19 ++++++++++---------
  qemu-sockets.c     |    1 +
  ui/keymaps.c       |   12 +++++++-----
  5 files changed, 22 insertions(+), 14 deletions(-)