Fix memory leaks in BIO_dup_chain()
[openssl.git] / crypto / bio / bio_lib.c
index 5267010cb0d7ff51230e49c34bd72328f8ffe8b7..cc859da74012e25adb220c9d75dfd4e1e181e29b 100644 (file)
 #include <stdio.h>
 #include <errno.h>
 #include <openssl/crypto.h>
 #include <stdio.h>
 #include <errno.h>
 #include <openssl/crypto.h>
-#include "cryptlib.h"
+#include "internal/cryptlib.h"
 #include <openssl/bio.h>
 #include <openssl/stack.h>
 
 BIO *BIO_new(BIO_METHOD *method)
 {
 #include <openssl/bio.h>
 #include <openssl/stack.h>
 
 BIO *BIO_new(BIO_METHOD *method)
 {
-    BIO *ret = NULL;
+    BIO *ret = OPENSSL_malloc(sizeof(*ret));
 
 
-    ret = (BIO *)OPENSSL_malloc(sizeof(BIO));
     if (ret == NULL) {
         BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
         return (NULL);
     if (ret == NULL) {
         BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
         return (NULL);
@@ -536,8 +535,10 @@ BIO *BIO_dup_chain(BIO *in)
 
         /* copy app data */
         if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
 
         /* copy app data */
         if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
-                                &bio->ex_data))
+                                &bio->ex_data)) {
+            BIO_free(new_bio);
             goto err;
             goto err;
+        }
 
         if (ret == NULL) {
             eoc = new_bio;
 
         if (ret == NULL) {
             eoc = new_bio;
@@ -549,8 +550,8 @@ BIO *BIO_dup_chain(BIO *in)
     }
     return (ret);
  err:
     }
     return (ret);
  err:
-    if (ret != NULL)
-        BIO_free(ret);
+    BIO_free_all(ret);
+
     return (NULL);
 }
 
     return (NULL);
 }
 
@@ -590,5 +591,3 @@ unsigned long BIO_number_written(BIO *bio)
         return bio->num_write;
     return 0;
 }
         return bio->num_write;
     return 0;
 }
-
-IMPLEMENT_STACK_OF(BIO)