(d2i_SSL_SESSION): Fix a memory leak that occurs on error.

Change-Id: I5424617f0b12937f092698d35a99316eb73a6c35
Reviewed-on: https://boringssl-review.googlesource.com/4390
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/ssl/ssl_asn1.c b/ssl/ssl_asn1.c
index e4a7f0d..3bca0c9 100644
--- a/ssl/ssl_asn1.c
+++ b/ssl/ssl_asn1.c
@@ -410,7 +410,7 @@
 }
 
 SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
-  SSL_SESSION *ret = NULL;
+  SSL_SESSION *ret, *allocated = NULL;
   CBS cbs, session, cipher, session_id, master_key;
   CBS peer, sid_ctx, peer_sha256, original_handshake_hash;
   int has_peer, has_peer_sha256, extended_master_secret;
@@ -420,8 +420,8 @@
   if (a && *a) {
     ret = *a;
   } else {
-    ret = SSL_SESSION_new();
-    if (ret == NULL) {
+    ret = allocated = SSL_SESSION_new();
+    if (allocated == NULL) {
       goto err;
     }
   }
@@ -585,8 +585,8 @@
   return ret;
 
 err:
-  if (a && *a != ret) {
-    SSL_SESSION_free(ret);
+  if (allocated) {
+    SSL_SESSION_free(allocated);
   }
   return NULL;
 }