[PATCH libICE 06/13] check malloc return

walter harms wharms at bfs.de
Wed Oct 18 15:58:58 UTC 2017



 check malloc return
 failed mallocs will cause segfaults, so add check
 also free already allocated memory

 Signed-off-by: Walter Harms <wharms at bfs.de>
---
 src/connect.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/connect.c b/src/connect.c
index 276a356..b61449e 100644
--- a/src/connect.c
+++ b/src/connect.c
@@ -193,8 +193,8 @@ IceOpenConnection (

     iceConn->connect_to_me = NULL;
     iceConn->protosetup_to_me = NULL;
-
-    if ((iceConn->inbuf = iceConn->inbufptr = malloc (ICE_INBUFSIZE)) == NULL)
+    iceConn->inbuf = iceConn->inbufptr = malloc (ICE_INBUFSIZE);
+    if ( iceConn->inbuf == NULL)
     {
 	_IceFreeConnection (iceConn);
 	strncpy (errorStringRet, "Can't malloc", errorLength);
@@ -202,9 +202,10 @@ IceOpenConnection (
     }

     iceConn->inbufmax = iceConn->inbuf + ICE_INBUFSIZE;
-
-    if ((iceConn->outbuf = iceConn->outbufptr = calloc (1, ICE_OUTBUFSIZE)) == NULL)
+    iceConn->outbuf = iceConn->outbufptr = calloc (1, ICE_OUTBUFSIZE);
+    if ( iceConn->outbuf == NULL)
     {
+        free(iceConn->inbuf);
 	_IceFreeConnection (iceConn);
 	strncpy (errorStringRet, "Can't malloc", errorLength);
 	return (NULL);
@@ -225,6 +226,14 @@ IceOpenConnection (
     iceConn->connect_to_you = malloc (sizeof (_IceConnectToYouInfo));
     iceConn->connect_to_you->auth_active = 0;

+    if (!iceConn->connect_to_you) {
+        free(iceConn->outbuf);
+        free(iceConn->inbuf);
+	_IceFreeConnection (iceConn);
+	strncpy (errorStringRet, "Can't malloc", errorLength);
+	return (NULL);
+    }
+
     /*
      * Send our byte order.
      */
@@ -467,6 +476,8 @@ ConnectToPeer (char *networkIdsList, char **actualConnectionRet)
     else
     {
        address = malloc (len + 1);
+       if (!address)
+	 return (NULL);
        address_size = len;
     }

-- 
2.1.4



More information about the xorg-devel mailing list