Subject: [PATCH 02/46] savevm: Fix potential memory leak

From: Nigel Croxon <[email-masked]>
Date: Thu, 14 Nov 2013 22:52:38 +0100

RH-Author: Nigel Croxon <[email-masked]>
Message-id: <[email-masked]>
Patchwork-id: 55690
O-Subject: [RHEL7.0 PATCH 02/42] savevm: Fix potential memory leak
Bugzilla: 1011720
RH-Acked-by: Orit Wasserman <[email-masked]>
RH-Acked-by: Amit Shah <[email-masked]>
RH-Acked-by: Paolo Bonzini <[email-masked]>

Bugzilla: 1011720
https://bugzilla.redhat.com/show_bug.cgi?id=1011720

>From commit ID:
commit 4f080057767a684aab60998659e18f98c5fbefc7
Author: Stefan Weil <[email-masked]>
Date:   Sun Jun 16 13:33:05 2013 +0200

    savevm: Fix potential memory leak

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

    Signed-off-by: Stefan Weil <[email-masked]>
    Signed-off-by: Michael Tokarev <[email-masked]>
---
 savevm.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

Signed-off-by: Michal Novotny <[email-masked]>
---
 savevm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/savevm.c b/savevm.c
index 4d12d92..4d898af 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);
-- 
1.7.11.7