asn1: properly clean up on failed BIO creation
authorPauli <pauli@openssl.org>
Mon, 21 Jun 2021 00:33:10 +0000 (10:33 +1000)
committerPauli <pauli@openssl.org>
Tue, 22 Jun 2021 02:43:21 +0000 (12:43 +1000)
Fixes coverity 1486070 through 1486077 and 1486079

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15841)

crypto/asn1/asn1_parse.c

index a131713d73781e2a8ee1531d70a848a5abc3c36c..04d7ef66cfc917536022e882f5469a8afb1af6ab 100644 (file)
@@ -27,6 +27,7 @@ static int asn1_print_info(BIO *bp, long offset, int depth, int hl, long len,
     int pop_f_prefix = 0;
     long saved_indent = -1;
     int i = 0;
+    BIO *bio = NULL;
 
     if (constructed & V_ASN1_CONSTRUCTED)
         p = "cons: ";
@@ -43,7 +44,8 @@ static int asn1_print_info(BIO *bp, long offset, int depth, int hl, long len,
     }
     if (bp != NULL) {
         if (BIO_set_prefix(bp, str) <= 0) {
-            if ((bp = BIO_push(BIO_new(BIO_f_prefix()), bp)) == NULL)
+            if ((bio = BIO_new(BIO_f_prefix())) == NULL
+                    || (bp = BIO_push(bio, bp)) == NULL)
                 goto err;
             pop_f_prefix = 1;
         }
@@ -72,10 +74,9 @@ static int asn1_print_info(BIO *bp, long offset, int depth, int hl, long len,
  err:
     if (saved_indent >= 0)
         BIO_set_indent(bp, saved_indent);
-    if (pop_f_prefix) {
+    if (pop_f_prefix)
         BIO_pop(bp);
-        BIO_free(bp);
-    }
+    BIO_free(bio);
     return i;
 }