If errno is ENXIO in BSS_new_file(), set BIO_R_NO_SUCH_FILE
[oweals/openssl.git] / crypto / bio / bio_lib.c
index 0b111c663c3fc04d7832206549610ee33fddbb00..62392c3a52157f7e2b562c9d54ca35e4de689d71 100644 (file)
 #include <openssl/crypto.h>
 #include "bio_lcl.h"
 #include "internal/cryptlib.h"
-#include <openssl/stack.h>
 
 BIO *BIO_new(const BIO_METHOD *method)
 {
-    BIO *ret = OPENSSL_malloc(sizeof(*ret));
+    BIO *bio = OPENSSL_zalloc(sizeof(*bio));
 
-    if (ret == NULL) {
+    if (bio == NULL) {
         BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
         return (NULL);
     }
-    if (!BIO_set(ret, method)) {
-        OPENSSL_free(ret);
-        ret = NULL;
-    }
-    return (ret);
-}
 
-int BIO_set(BIO *bio, const BIO_METHOD *method)
-{
     bio->method = method;
-    bio->callback = NULL;
-    bio->cb_arg = NULL;
-    bio->init = 0;
     bio->shutdown = 1;
-    bio->flags = 0;
-    bio->retry_reason = 0;
-    bio->num = 0;
-    bio->ptr = NULL;
-    bio->prev_bio = NULL;
-    bio->next_bio = NULL;
     bio->references = 1;
-    bio->num_read = 0L;
-    bio->num_write = 0L;
+
     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
-        return 0;
+        goto err;
 
     bio->lock = CRYPTO_THREAD_lock_new();
     if (bio->lock == NULL) {
-        BIOerr(BIO_F_BIO_SET, ERR_R_MALLOC_FAILURE);
+        BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
-        return 0;
+        goto err;
     }
 
-    if (method->create != NULL) {
-        if (!method->create(bio)) {
-            CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
-            CRYPTO_THREAD_lock_free(bio->lock);
-            return 0;
-        }
+    if (method->create != NULL && !method->create(bio)) {
+        BIOerr(BIO_F_BIO_NEW, ERR_R_INIT_FAIL);
+        CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
+        CRYPTO_THREAD_lock_free(bio->lock);
+        goto err;
     }
 
-    return 1;
+    return bio;
+
+err:
+    OPENSSL_free(bio);
+    return NULL;
 }
 
 int BIO_free(BIO *a)
@@ -611,4 +595,6 @@ void bio_cleanup(void)
     CRYPTO_THREAD_lock_free(bio_lookup_lock);
     bio_lookup_lock = NULL;
 #endif
+    CRYPTO_THREAD_lock_free(bio_type_lock);
+    bio_type_lock = NULL;
 }