Simplify built-in BIOs slightly.

The free callbacks can assume their inputs are non-NULL. They're only
called from BIOs of the corresponding method, which means the BIO must
exist. Also new callbacks that leave everything zero-initialized are
no-ops and can be omitted.

This removes the weird thing where the built-in free functions were
fallible. Although the int return is still necessary for compatibility
with external BIOs.

Change-Id: I91e2101efc7c77c703cb649df1490bc9f515f0fd
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/48846
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/bio/bio_mem.c b/crypto/bio/bio_mem.c
index 08dd6e9..f40a9a7 100644
--- a/crypto/bio/bio_mem.c
+++ b/crypto/bio/bio_mem.c
@@ -116,17 +116,11 @@
 }
 
 static int mem_free(BIO *bio) {
-  BUF_MEM *b;
-
-  if (bio == NULL) {
-    return 0;
-  }
-
   if (!bio->shutdown || !bio->init || bio->ptr == NULL) {
     return 1;
   }
 
-  b = (BUF_MEM *)bio->ptr;
+  BUF_MEM *b = (BUF_MEM *)bio->ptr;
   if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
     b->data = NULL;
   }
diff --git a/crypto/bio/connect.c b/crypto/bio/connect.c
index b8afa61..3b65acf 100644
--- a/crypto/bio/connect.c
+++ b/crypto/bio/connect.c
@@ -320,7 +320,7 @@
   bio->init = 0;
   bio->num = -1;
   bio->flags = 0;
-  bio->ptr = (char *)BIO_CONNECT_new();
+  bio->ptr = BIO_CONNECT_new();
   return bio->ptr != NULL;
 }
 
@@ -340,10 +340,6 @@
 }
 
 static int conn_free(BIO *bio) {
-  if (bio == NULL) {
-    return 0;
-  }
-
   if (bio->shutdown) {
     conn_close_socket(bio);
   }
diff --git a/crypto/bio/fd.c b/crypto/bio/fd.c
index d4e6918..349ee9d 100644
--- a/crypto/bio/fd.c
+++ b/crypto/bio/fd.c
@@ -146,10 +146,6 @@
 }
 
 static int fd_free(BIO *bio) {
-  if (bio == NULL) {
-    return 0;
-  }
-
   if (bio->shutdown) {
     if (bio->init) {
       BORINGSSL_CLOSE(bio->num);
diff --git a/crypto/bio/file.c b/crypto/bio/file.c
index 15feb9d..835d661 100644
--- a/crypto/bio/file.c
+++ b/crypto/bio/file.c
@@ -126,13 +126,7 @@
   return ret;
 }
 
-static int file_new(BIO *bio) { return 1; }
-
 static int file_free(BIO *bio) {
-  if (bio == NULL) {
-    return 0;
-  }
-
   if (!bio->shutdown) {
     return 1;
   }
@@ -279,7 +273,7 @@
     BIO_TYPE_FILE,   "FILE pointer",
     file_write,      file_read,
     NULL /* puts */, file_gets,
-    file_ctrl,       file_new,
+    file_ctrl,       NULL /* create */,
     file_free,       NULL /* callback_ctrl */,
 };
 
diff --git a/crypto/bio/pair.c b/crypto/bio/pair.c
index 03f60b7..a1a9c9c 100644
--- a/crypto/bio/pair.c
+++ b/crypto/bio/pair.c
@@ -127,12 +127,7 @@
 }
 
 static int bio_free(BIO *bio) {
-  struct bio_bio_st *b;
-
-  if (bio == NULL) {
-    return 0;
-  }
-  b = bio->ptr;
+  struct bio_bio_st *b = bio->ptr;
 
   assert(b != NULL);
 
diff --git a/crypto/bio/socket.c b/crypto/bio/socket.c
index 081ce01..679959e 100644
--- a/crypto/bio/socket.c
+++ b/crypto/bio/socket.c
@@ -81,19 +81,7 @@
 }
 #endif
 
-static int sock_new(BIO *bio) {
-  bio->init = 0;
-  bio->num = 0;
-  bio->ptr = NULL;
-  bio->flags = 0;
-  return 1;
-}
-
 static int sock_free(BIO *bio) {
-  if (bio == NULL) {
-    return 0;
-  }
-
   if (bio->shutdown) {
     if (bio->init) {
       closesocket(bio->num);
@@ -105,17 +93,15 @@
 }
 
 static int sock_read(BIO *b, char *out, int outl) {
-  int ret = 0;
-
   if (out == NULL) {
     return 0;
   }
 
   bio_clear_socket_error();
 #if defined(OPENSSL_WINDOWS)
-  ret = recv(b->num, out, outl, 0);
+  int ret = recv(b->num, out, outl, 0);
 #else
-  ret = read(b->num, out, outl);
+  int ret = read(b->num, out, outl);
 #endif
   BIO_clear_retry_flags(b);
   if (ret <= 0) {
@@ -186,7 +172,7 @@
     BIO_TYPE_SOCKET, "socket",
     sock_write,      sock_read,
     NULL /* puts */, NULL /* gets, */,
-    sock_ctrl,       sock_new,
+    sock_ctrl,       NULL /* create */,
     sock_free,       NULL /* callback_ctrl */,
 };