ddf19c
From bcb6107f98d7b1edf687d7afd552a4528b7e673b Mon Sep 17 00:00:00 2001
ddf19c
From: jmaloy <jmaloy@redhat.com>
ddf19c
Date: Tue, 12 May 2020 21:15:13 +0100
ddf19c
Subject: [PATCH 2/7] Don't leak memory when reallocation fails.
ddf19c
MIME-Version: 1.0
ddf19c
Content-Type: text/plain; charset=UTF-8
ddf19c
Content-Transfer-Encoding: 8bit
ddf19c
ddf19c
RH-Author: jmaloy <jmaloy@redhat.com>
ddf19c
Message-id: <20200512211514.1398384-2-jmaloy@redhat.com>
ddf19c
Patchwork-id: 96412
ddf19c
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 1/2] Don't leak memory when reallocation fails.
ddf19c
Bugzilla: 1749737
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
RH-Acked-by: Thomas Huth <thuth@redhat.com>
ddf19c
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
ddf19c
From: Jindrich Novy <jnovy@redhat.com>
ddf19c
ddf19c
Signed-off-by: Jindrich Novy <jnovy@redhat.com>
ddf19c
[ Marc-André - modified to use a temporary variable ]
ddf19c
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
ddf19c
(cherry picked from libslirp commit d171af3732a0610a25334b06b77fa547bd677918)
ddf19c
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
ddf19c
ddf19c
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ddf19c
---
ddf19c
 slirp/src/sbuf.c | 11 +++++++----
ddf19c
 1 file changed, 7 insertions(+), 4 deletions(-)
ddf19c
ddf19c
diff --git a/slirp/src/sbuf.c b/slirp/src/sbuf.c
ddf19c
index abced48..0569c34 100644
ddf19c
--- a/slirp/src/sbuf.c
ddf19c
+++ b/slirp/src/sbuf.c
ddf19c
@@ -39,13 +39,16 @@ void sbreserve(struct sbuf *sb, int size)
ddf19c
     if (sb->sb_data) {
ddf19c
         /* Already alloced, realloc if necessary */
ddf19c
         if (sb->sb_datalen != size) {
ddf19c
-            sb->sb_wptr = sb->sb_rptr = sb->sb_data =
ddf19c
-                (char *)realloc(sb->sb_data, size);
ddf19c
+            char *new = realloc(sb->sb_data, size);
ddf19c
             sb->sb_cc = 0;
ddf19c
-            if (sb->sb_wptr)
ddf19c
+            if (new) {
ddf19c
+                sb->sb_data = sb->sb_wptr = sb->sb_rptr = new;
ddf19c
                 sb->sb_datalen = size;
ddf19c
-            else
ddf19c
+            } else {
ddf19c
+                free(sb->sb_data);
ddf19c
+                sb->sb_data = sb->sb_wptr = sb->sb_rptr = NULL;
ddf19c
                 sb->sb_datalen = 0;
ddf19c
+            }
ddf19c
         }
ddf19c
     } else {
ddf19c
         sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)malloc(size);
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c