Patchwork [PULL,05/17] savevm: Fix potential memory leak

login
register
mail settings
Submitter Michael Tokarev
Date June 22, 2013, 9:24 a.m.
Message ID <1371893076-9643-6-git-send-email-mjt@msgid.tls.msk.ru>
Download mbox | patch
Permalink /patch/253395/
State New
Headers show

Comments

Michael Tokarev - June 22, 2013, 9:24 a.m.
From: Stefan Weil <sw@weilnetz.de>

The leak was reported by cppcheck. Fix it by moving the g_malloc0 after
the argument validity check.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
---
 savevm.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Patch

diff --git a/savevm.c b/savevm.c
index ff5ece6..48cc2a9 100644
--- a/savevm.c
+++ b/savevm.c
@@ -479,7 +479,7 @@  static const QEMUFileOps socket_write_ops = {
 
 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
 {
-    QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
+    QEMUFileSocket *s;
 
     if (mode == NULL ||
         (mode[0] != 'r' && mode[0] != 'w') ||
@@ -488,6 +488,7 @@  QEMUFile *qemu_fopen_socket(int fd, const char *mode)
         return NULL;
     }
 
+    s = g_malloc0(sizeof(QEMUFileSocket));
     s->fd = fd;
     if (mode[0] == 'w') {
         qemu_set_block(s->fd);