Skip to content

Commit 5f450f3

Browse files
danbevtargos
authored andcommitted
crypto: replace goto SSL_CTX_use_certificate_chain
This commit removes the goto statements in SSL_CTX_use_certificate_chain by using a unique_ptr. PR-URL: #23113 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b523f13 commit 5f450f3

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

src/node_crypto.cc

+14-25
Original file line numberDiff line numberDiff line change
@@ -647,50 +647,39 @@ int SSL_CTX_use_certificate_chain(SSL_CTX* ctx,
647647
if (!x)
648648
return 0;
649649

650-
// TODO(addaleax): Turn this into smart pointer as well.
651-
X509* extra = nullptr;
652-
int ret = 0;
653650
unsigned long err = 0; // NOLINT(runtime/int)
654651

655652
StackOfX509 extra_certs(sk_X509_new_null());
656653
if (!extra_certs)
657-
goto done;
654+
return 0;
658655

659-
while ((extra = PEM_read_bio_X509(in.get(),
656+
while (X509Pointer extra {PEM_read_bio_X509(in.get(),
660657
nullptr,
661658
NoPasswordCallback,
662-
nullptr))) {
663-
if (sk_X509_push(extra_certs.get(), extra))
659+
nullptr)}) {
660+
if (sk_X509_push(extra_certs.get(), extra.get())) {
661+
extra.release();
664662
continue;
663+
}
665664

666-
// Failure, free all certs
667-
goto done;
665+
return 0;
668666
}
669-
extra = nullptr;
670667

671668
// When the while loop ends, it's usually just EOF.
672669
err = ERR_peek_last_error();
673670
if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
674671
ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
675672
ERR_clear_error();
676-
} else {
673+
} else {
677674
// some real error
678-
goto done;
675+
return 0;
679676
}
680677

681-
ret = SSL_CTX_use_certificate_chain(ctx,
682-
std::move(x),
683-
extra_certs.get(),
684-
cert,
685-
issuer);
686-
if (!ret)
687-
goto done;
688-
689-
done:
690-
if (extra != nullptr)
691-
X509_free(extra);
692-
693-
return ret;
678+
return SSL_CTX_use_certificate_chain(ctx,
679+
std::move(x),
680+
extra_certs.get(),
681+
cert,
682+
issuer);
694683
}
695684

696685

0 commit comments

Comments
 (0)