From f61886f55633dd60a7592ba5851b7c730d887c41 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 11 Dec 2022 15:45:07 +0100 Subject: [PATCH] src: simplify NodeBIO::GetMethod initialization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make its initialization self-contained to avoid unnecessarily breaking encapsulation. PR-URL: https://github.com/nodejs/node/pull/45799 Reviewed-By: Yagiz Nizipli Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Minwoo Jung Reviewed-By: Tobias Nießen --- src/crypto/crypto_bio.cc | 12 +++++------- src/crypto/crypto_bio.h | 2 -- src/crypto/crypto_util.cc | 2 -- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/crypto/crypto_bio.cc b/src/crypto/crypto_bio.cc index 25ea640ad83077..099b11ee72520d 100644 --- a/src/crypto/crypto_bio.cc +++ b/src/crypto/crypto_bio.cc @@ -223,12 +223,9 @@ long NodeBIO::Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int) const BIO_METHOD* NodeBIO::GetMethod() { - // This is called from InitCryptoOnce() to avoid race conditions during - // initialization. - static BIO_METHOD* method = nullptr; - - if (method == nullptr) { - method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer"); + // Static initialization ensures that this is safe to use concurrently. + static const BIO_METHOD* method = [&]() { + BIO_METHOD* method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer"); BIO_meth_set_write(method, Write); BIO_meth_set_read(method, Read); BIO_meth_set_puts(method, Puts); @@ -236,7 +233,8 @@ const BIO_METHOD* NodeBIO::GetMethod() { BIO_meth_set_ctrl(method, Ctrl); BIO_meth_set_create(method, New); BIO_meth_set_destroy(method, Free); - } + return method; + }(); return method; } diff --git a/src/crypto/crypto_bio.h b/src/crypto/crypto_bio.h index b25980ad1fae78..7587a353f11cde 100644 --- a/src/crypto/crypto_bio.h +++ b/src/crypto/crypto_bio.h @@ -182,8 +182,6 @@ class NodeBIO : public MemoryRetainer { int eof_return_ = -1; Buffer* read_head_ = nullptr; Buffer* write_head_ = nullptr; - - friend void node::crypto::InitCryptoOnce(); }; } // namespace crypto diff --git a/src/crypto/crypto_util.cc b/src/crypto/crypto_util.cc index f1bb7a9c7aadbc..28eb1bd6b7dc83 100644 --- a/src/crypto/crypto_util.cc +++ b/src/crypto/crypto_util.cc @@ -184,8 +184,6 @@ void InitCryptoOnce() { ERR_load_ENGINE_strings(); ENGINE_load_builtin_engines(); #endif // !OPENSSL_NO_ENGINE - - NodeBIO::GetMethod(); } void GetFipsCrypto(const FunctionCallbackInfo& args) {