Blob Blame Raw
From c012dc9b501d96a2ff54a8a7a182726043b69aeb Mon Sep 17 00:00:00 2001
From: jmaloy <jmaloy@redhat.com>
Date: Tue, 12 May 2020 21:15:14 +0100
Subject: [PATCH 3/7] Replace remaining malloc/free user with glib
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

RH-Author: jmaloy <jmaloy@redhat.com>
Message-id: <20200512211514.1398384-3-jmaloy@redhat.com>
Patchwork-id: 96413
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 2/2] Replace remaining malloc/free user with glib
Bugzilla: 1749737
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
RH-Acked-by: Thomas Huth <thuth@redhat.com>
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>

From: Marc-André Lureau <marcandre.lureau@redhat.com>

glib mem functions are already used in various places. Let's not mix
the two, and instead abort on OOM conditions.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
(cherry picked from libslirp commit 3a494648526be4eb96cba739a816a60e933ffd14)
Signed-off-by: Jon Maloy <jmaloy@redhat.com>

Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
---
 slirp/src/sbuf.c     | 21 ++++++---------------
 slirp/src/socket.c   |  2 +-
 slirp/src/tcp_subr.c |  8 ++------
 3 files changed, 9 insertions(+), 22 deletions(-)

diff --git a/slirp/src/sbuf.c b/slirp/src/sbuf.c
index 0569c34..eab87f3 100644
--- a/slirp/src/sbuf.c
+++ b/slirp/src/sbuf.c
@@ -9,7 +9,7 @@ static void sbappendsb(struct sbuf *sb, struct mbuf *m);
 
 void sbfree(struct sbuf *sb)
 {
-    free(sb->sb_data);
+    g_free(sb->sb_data);
 }
 
 bool sbdrop(struct sbuf *sb, int num)
@@ -39,24 +39,15 @@ void sbreserve(struct sbuf *sb, int size)
     if (sb->sb_data) {
         /* Already alloced, realloc if necessary */
         if (sb->sb_datalen != size) {
-            char *new = realloc(sb->sb_data, size);
+            char *new = g_realloc(sb->sb_data, size);
             sb->sb_cc = 0;
-            if (new) {
-                sb->sb_data = sb->sb_wptr = sb->sb_rptr = new;
-                sb->sb_datalen = size;
-            } else {
-                free(sb->sb_data);
-                sb->sb_data = sb->sb_wptr = sb->sb_rptr = NULL;
-                sb->sb_datalen = 0;
-            }
+            sb->sb_data = sb->sb_wptr = sb->sb_rptr = new;
+            sb->sb_datalen = size;
         }
     } else {
-        sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)malloc(size);
+        sb->sb_wptr = sb->sb_rptr = sb->sb_data = g_malloc(size);
         sb->sb_cc = 0;
-        if (sb->sb_wptr)
-            sb->sb_datalen = size;
-        else
-            sb->sb_datalen = 0;
+        sb->sb_datalen = size;
     }
 }
 
diff --git a/slirp/src/socket.c b/slirp/src/socket.c
index 34daffc..ace18bf 100644
--- a/slirp/src/socket.c
+++ b/slirp/src/socket.c
@@ -95,7 +95,7 @@ void sofree(struct socket *so)
         remque(so); /* crashes if so is not in a queue */
 
     if (so->so_tcpcb) {
-        free(so->so_tcpcb);
+        g_free(so->so_tcpcb);
     }
     g_free(so);
 }
diff --git a/slirp/src/tcp_subr.c b/slirp/src/tcp_subr.c
index 26d4ead..4e5a801 100644
--- a/slirp/src/tcp_subr.c
+++ b/slirp/src/tcp_subr.c
@@ -255,11 +255,7 @@ struct tcpcb *tcp_newtcpcb(struct socket *so)
 {
     register struct tcpcb *tp;
 
-    tp = (struct tcpcb *)malloc(sizeof(*tp));
-    if (tp == NULL)
-        return ((struct tcpcb *)0);
-
-    memset((char *)tp, 0, sizeof(struct tcpcb));
+    tp = g_new0(struct tcpcb, 1);
     tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
     tp->t_maxseg = (so->so_ffamily == AF_INET) ? TCP_MSS : TCP6_MSS;
 
@@ -330,7 +326,7 @@ struct tcpcb *tcp_close(struct tcpcb *tp)
         remque(tcpiphdr2qlink(tcpiphdr_prev(t)));
         m_free(m);
     }
-    free(tp);
+    g_free(tp);
     so->so_tcpcb = NULL;
     /* clobber input socket cache if we're closing the cached connection */
     if (so == slirp->tcp_last_so)
-- 
1.8.3.1