PR: 2457
[openssl.git] / ssl / bio_ssl.c
index 0f86c03057371daa16c1654ea3b2fe936972a9f3..af319af302a1bc4a56f649e0522d14a6f43f6fdb 100644 (file)
@@ -398,17 +398,19 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
                        }
                break;
        case BIO_CTRL_POP:
-               /* ugly bit of a hack */
-               if (ssl->rbio != ssl->wbio) /* we are in trouble :-( */
+               /* Only detach if we are the BIO explicitly being popped */
+               if (b == ptr)
                        {
-                       BIO_free_all(ssl->wbio);
-                       }
-               if (b->next_bio != NULL)
-                       {
-                       CRYPTO_add(&b->next_bio->references,1,CRYPTO_LOCK_BIO);
+                       /* Shouldn't happen in practice because the
+                        * rbio and wbio are the same when pushed.
+                        */
+                       if (ssl->rbio != ssl->wbio)
+                               BIO_free_all(ssl->wbio);
+                       if (b->next_bio != NULL)
+                               CRYPTO_add(&b->next_bio->references,-1,CRYPTO_LOCK_BIO);
+                       ssl->wbio=NULL;
+                       ssl->rbio=NULL;
                        }
-               ssl->wbio=NULL;
-               ssl->rbio=NULL;
                break;
        case BIO_C_DO_STATE_MACHINE:
                BIO_clear_retry_flags(b);
@@ -456,7 +458,7 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
        case BIO_CTRL_SET_CALLBACK:
                {
 #if 0 /* FIXME: Should this be used?  -- Richard Levitte */
-               BIOerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+               SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
                ret = -1;
 #else
                ret=0;
@@ -543,7 +545,6 @@ BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
        return(ret);
 err:
        if (con != NULL) BIO_free(con);
-       if (ret != NULL) BIO_free(ret);
        return(NULL);
        }