test/bio_base64_test.c: Add check for BIO_new()

Add check for the return value of BIO_new() to avoid NULL pointer dereference.

Fixes: 0cd9dd703e ("Improve base64 BIO correctness and error reporting")
Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>

Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Paul Dale <paul.dale@oracle.com>
Reviewed-by: Frederik Wedel-Heinen <fwh.openssl@gmail.com>
MergeDate: Mon Jan 12 18:42:15 2026
(Merged from https://github.com/openssl/openssl/pull/27993)
This commit is contained in:
Jiasheng Jiang
2025-07-08 18:44:20 +00:00
committed by Tomas Mraz
parent 669815e846
commit ffb5ca705b

View File

@@ -182,12 +182,12 @@ static int genb64(char *prefix, char *suffix, unsigned const char *buf,
static int test_bio_base64_run(test_case *t, int llen, int wscnt) static int test_bio_base64_run(test_case *t, int llen, int wscnt)
{ {
unsigned char *raw; unsigned char *raw = NULL;
unsigned char *out; unsigned char *out = NULL;
unsigned out_len; unsigned out_len;
char *encoded = NULL; char *encoded = NULL;
int elen; int elen;
BIO *bio, *b64; BIO *bio = NULL, *b64 = NULL;
int n, n1, n2; int n, n1, n2;
int ret; int ret;
@@ -208,19 +208,17 @@ static int test_bio_base64_run(test_case *t, int llen, int wscnt)
out_len = t->bytes + 1024; out_len = t->bytes + 1024;
out = OPENSSL_malloc(out_len); out = OPENSSL_malloc(out_len);
if (out == NULL) { if (out == NULL) {
OPENSSL_free(raw);
TEST_error("out of memory"); TEST_error("out of memory");
return -1; ret = -1;
goto end;
} }
elen = genb64(t->prefix, t->suffix, raw, t->bytes, t->trunc, t->encoded, elen = genb64(t->prefix, t->suffix, raw, t->bytes, t->trunc, t->encoded,
llen, wscnt, &encoded); llen, wscnt, &encoded);
if (elen < 0 || (bio = BIO_new(BIO_s_mem())) == NULL) { if (elen < 0 || (bio = BIO_new(BIO_s_mem())) == NULL) {
OPENSSL_free(raw);
OPENSSL_free(out);
OPENSSL_free(encoded);
TEST_error("out of memory"); TEST_error("out of memory");
return -1; ret = -1;
goto end;
} }
if (t->retry) if (t->retry)
BIO_set_mem_eof_return(bio, EOF_RETURN); BIO_set_mem_eof_return(bio, EOF_RETURN);
@@ -238,7 +236,10 @@ static int test_bio_base64_run(test_case *t, int llen, int wscnt)
if (n1 > 0) if (n1 > 0)
BIO_write(bio, encoded, n1); BIO_write(bio, encoded, n1);
b64 = BIO_new(BIO_f_base64()); if (!TEST_ptr(b64 = BIO_new(BIO_f_base64()))) {
ret = -1;
goto end;
}
if (t->no_nl) if (t->no_nl)
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
BIO_push(b64, bio); BIO_push(b64, bio);
@@ -296,11 +297,12 @@ static int test_bio_base64_run(test_case *t, int llen, int wscnt)
ret = -1; ret = -1;
} }
BIO_free_all(b64); end:
OPENSSL_free(out); BIO_free(bio);
BIO_free(b64);
OPENSSL_free(raw); OPENSSL_free(raw);
OPENSSL_free(out);
OPENSSL_free(encoded); OPENSSL_free(encoded);
return ret; return ret;
} }