realloc of NULL is like malloc
authorRich Salz <rsalz@openssl.org>
Tue, 28 Apr 2015 20:34:52 +0000 (16:34 -0400)
committerRich Salz <rsalz@openssl.org>
Tue, 28 Apr 2015 20:34:52 +0000 (16:34 -0400)
ANSI C, and OpenSSL's malloc wrapper do this, also.

Reviewed-by: Richard Levitte <levitte@openssl.org>
crypto/asn1/a_bitstr.c
crypto/asn1/asn1_lib.c
crypto/asn1/f_enum.c
crypto/asn1/f_int.c
crypto/asn1/f_string.c
crypto/bio/b_sock.c
crypto/buffer/buffer.c
crypto/err/err.c

index 8a9e17cb27c3e7c78e872869da065abd663607e2..ba243f1a5f5c47b1dc09fff8632fa7c7a7cc0186 100644 (file)
@@ -205,10 +205,7 @@ int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
     if ((a->length < (w + 1)) || (a->data == NULL)) {
         if (!value)
             return (1);         /* Don't need to set */
     if ((a->length < (w + 1)) || (a->data == NULL)) {
         if (!value)
             return (1);         /* Don't need to set */
-        if (a->data == NULL)
-            c = OPENSSL_malloc(w + 1);
-        else
-            c = OPENSSL_realloc_clean(a->data, a->length, w + 1);
+        c = OPENSSL_realloc_clean(a->data, a->length, w + 1);
         if (c == NULL) {
             ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
             return 0;
         if (c == NULL) {
             ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
             return 0;
index 97f1d23da830615a62ade00ac4f92c53c8faa24d..a892d7f063141539e2c9ef3387be45e238232c3d 100644 (file)
@@ -317,11 +317,7 @@ int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
     }
     if ((str->length < len) || (str->data == NULL)) {
         c = str->data;
     }
     if ((str->length < len) || (str->data == NULL)) {
         c = str->data;
-        if (c == NULL)
-            str->data = OPENSSL_malloc(len + 1);
-        else
-            str->data = OPENSSL_realloc(c, len + 1);
-
+        str->data = OPENSSL_realloc(c, len + 1);
         if (str->data == NULL) {
             ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
             str->data = c;
         if (str->data == NULL) {
             ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
             str->data = c;
index c623cdc7843ba4e1278350a3fb9d1039e015901a..2ec99a51a3417076c8812d962afae1b876805640 100644 (file)
@@ -151,10 +151,7 @@ int a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
         }
         i /= 2;
         if (num + i > slen) {
         }
         i /= 2;
         if (num + i > slen) {
-            if (s == NULL)
-                sp = OPENSSL_malloc((unsigned int)num + i * 2);
-            else
-                sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
+            sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
             if (sp == NULL) {
                 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED, ERR_R_MALLOC_FAILURE);
                 if (s != NULL)
             if (sp == NULL) {
                 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED, ERR_R_MALLOC_FAILURE);
                 if (s != NULL)
index 39c9a61e598121a09507a7807392ee5271753d32..f74252c45a4247a7a6ba228c7a1867d36fe584cb 100644 (file)
@@ -165,10 +165,7 @@ int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size)
         }
         i /= 2;
         if (num + i > slen) {
         }
         i /= 2;
         if (num + i > slen) {
-            if (s == NULL)
-                sp = OPENSSL_malloc((unsigned int)num + i * 2);
-            else
-                sp = OPENSSL_realloc_clean(s, slen, num + i * 2);
+            sp = OPENSSL_realloc_clean(s, slen, num + i * 2);
             if (sp == NULL) {
                 ASN1err(ASN1_F_A2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
                 if (s != NULL)
             if (sp == NULL) {
                 ASN1err(ASN1_F_A2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
                 if (s != NULL)
index 6cb4cfdff99a133e79272d56e60d26f6d9247515..53f8cf3a83a8cd4a36b5782dc107d446d89f0ae1 100644 (file)
@@ -157,10 +157,7 @@ int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size)
         }
         i /= 2;
         if (num + i > slen) {
         }
         i /= 2;
         if (num + i > slen) {
-            if (s == NULL)
-                sp = OPENSSL_malloc((unsigned int)num + i * 2);
-            else
-                sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
+            sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
             if (sp == NULL) {
                 ASN1err(ASN1_F_A2I_ASN1_STRING, ERR_R_MALLOC_FAILURE);
                 if (s != NULL)
             if (sp == NULL) {
                 ASN1err(ASN1_F_A2I_ASN1_STRING, ERR_R_MALLOC_FAILURE);
                 if (s != NULL)
index ca485d9c19f141bdbeafb80a936cc548a76687dc..a4fded5ec2a8517a7f6e572d5e5baeeb368c23f1 100644 (file)
@@ -673,12 +673,9 @@ int BIO_accept(int sock, char **addr)
             break;
         nl = strlen(h) + strlen(s) + 2;
         p = *addr;
             break;
         nl = strlen(h) + strlen(s) + 2;
         p = *addr;
-        if (p) {
+        if (p)
             *p = '\0';
             *p = '\0';
-            p = OPENSSL_realloc(p, nl);
-        } else {
-            p = OPENSSL_malloc(nl);
-        }
+        p = OPENSSL_realloc(p, nl);
         if (p == NULL) {
             BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE);
             goto end;
         if (p == NULL) {
             BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE);
             goto end;
index 0859974e79bb3a7adc68a5c4e06000272210988e..c77fdc5ccb97ca646c7ac73c698a90020c4d199e 100644 (file)
@@ -114,10 +114,7 @@ size_t BUF_MEM_grow(BUF_MEM *str, size_t len)
         return 0;
     }
     n = (len + 3) / 3 * 4;
         return 0;
     }
     n = (len + 3) / 3 * 4;
-    if (str->data == NULL)
-        ret = OPENSSL_malloc(n);
-    else
-        ret = OPENSSL_realloc(str->data, n);
+    ret = OPENSSL_realloc(str->data, n);
     if (ret == NULL) {
         BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);
         len = 0;
     if (ret == NULL) {
         BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);
         len = 0;
@@ -151,10 +148,7 @@ size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
         return 0;
     }
     n = (len + 3) / 3 * 4;
         return 0;
     }
     n = (len + 3) / 3 * 4;
-    if (str->data == NULL)
-        ret = OPENSSL_malloc(n);
-    else
-        ret = OPENSSL_realloc_clean(str->data, str->max, n);
+    ret = OPENSSL_realloc_clean(str->data, str->max, n);
     if (ret == NULL) {
         BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);
         len = 0;
     if (ret == NULL) {
         BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);
         len = 0;
index 4752148169b08426dc448004c2e74d70d555a873..ec7da22e5bbad3c97bfe82c0623dbd2624817716 100644 (file)
@@ -969,8 +969,8 @@ void ERR_add_error_vdata(int num, va_list args)
                 if (p == NULL) {
                     OPENSSL_free(str);
                     return;
                 if (p == NULL) {
                     OPENSSL_free(str);
                     return;
-                } else
-                    str = p;
+                }
+                str = p;
             }
             BUF_strlcat(str, a, (size_t)s + 1);
         }
             }
             BUF_strlcat(str, a, (size_t)s + 1);
         }