Fix some malloc failures in b_addr.c
authorMatt Caswell <matt@openssl.org>
Wed, 4 May 2016 10:14:48 +0000 (11:14 +0100)
committerMatt Caswell <matt@openssl.org>
Wed, 18 May 2016 09:47:15 +0000 (10:47 +0100)
There were some unchecked calls to OPENSSL_strdup().

Reviewed-by: Richard Levitte <levitte@openssl.org>
crypto/bio/b_addr.c

index bb6719eb2a3c24bb87515b5c49f5f860fea9f87d..3a9a00c121ab2d6c05ab8a0b3582dfb0fbc912ec 100644 (file)
@@ -228,21 +228,35 @@ static int addr_strings(const BIO_ADDR *ap, int numeric,
                          ntohs(BIO_ADDR_rawport(ap)));
         }
 
-        if (hostname)
+        if (hostname != NULL)
             *hostname = OPENSSL_strdup(host);
-        if (service)
+        if (service != NULL)
             *service = OPENSSL_strdup(serv);
     } else {
 #endif
-        if (hostname)
+        if (hostname != NULL)
             *hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr));
-        if (service) {
+        if (service != NULL) {
             char serv[6];        /* port is 16 bits => max 5 decimal digits */
             BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->s_in.sin_port));
             *service = OPENSSL_strdup(serv);
         }
     }
 
+    if ((hostname != NULL && *hostname == NULL)
+            || (service != NULL && *service == NULL)) {
+        if (hostname != NULL) {
+            OPENSSL_free(*hostname);
+            *hostname = NULL;
+        }
+        if (service != NULL) {
+            OPENSSL_free(*service);
+            *service = NULL;
+        }
+        BIOerr(BIO_F_ADDR_STRINGS, ERR_R_MALLOC_FAILURE);
+        return 0;
+    }
+
     return 1;
 }