Skip to contentSkip to conversation

savevm: Fix potential memory leak

PATCH In review Stefan Weil 1 participant 3 messages Jun 16, 2013 – Jul 2, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 savevm: Fix potential memory leak

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

---
 savevm.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
savevm.c +2 -1
index 2ce439f..b883714 100644
@@ -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);
Signature · 1 line
1.7.10.4
#1 lore raw

Am 16.06.2013 13:33, schrieb Stefan Weil:

Quoted 22 lines

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

---
 savevm.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
savevm.c +1 -1
index 2ce439f..b883714 100644
@@ -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') ||

savevm.c QEMUFile *qemu_fopen_socket(int fd, const char *mode) · line 494

Quoted 7 lines
savevm.c +1 -0
@@ -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);

Ping?

#2 lore raw

Am 02.07.2013 22:33, schrieb Stefan Weil:

Quoted 26 lines

Am 16.06.2013 13:33, schrieb Stefan Weil:

Quoted 25 lines

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

---
 savevm.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
savevm.c +1 -1
index 2ce439f..b883714 100644
@@ -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;
     }
 
Quoted 6 lines

savevm.c QEMUFile *qemu_fopen_socket(int fd, const char *mode) · line 494

Quoted 4 lines
savevm.c +1 -0
+    s = g_malloc0(sizeof(QEMUFileSocket));
     s->fd = fd;
     if (mode[0] == 'w') {
         qemu_set_block(s->fd);

Ping?

Sorry, that was bad bookkeeping on my side.
The patch was already applied.

Stefan