Skip to content

Commit 8e1138d

Browse files
addaleaxdanielleadams
authored andcommitted
src: simplify NodeBIO::GetMethod initialization
Make its initialization self-contained to avoid unnecessarily breaking encapsulation. PR-URL: #45799 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent b88ee54 commit 8e1138d

File tree

3 files changed

+5
-11
lines changed

3 files changed

+5
-11
lines changed

src/crypto/crypto_bio.cc

+5-7
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,18 @@ long NodeBIO::Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int)
223223

224224

225225
const BIO_METHOD* NodeBIO::GetMethod() {
226-
// This is called from InitCryptoOnce() to avoid race conditions during
227-
// initialization.
228-
static BIO_METHOD* method = nullptr;
229-
230-
if (method == nullptr) {
231-
method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
226+
// Static initialization ensures that this is safe to use concurrently.
227+
static const BIO_METHOD* method = [&]() {
228+
BIO_METHOD* method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
232229
BIO_meth_set_write(method, Write);
233230
BIO_meth_set_read(method, Read);
234231
BIO_meth_set_puts(method, Puts);
235232
BIO_meth_set_gets(method, Gets);
236233
BIO_meth_set_ctrl(method, Ctrl);
237234
BIO_meth_set_create(method, New);
238235
BIO_meth_set_destroy(method, Free);
239-
}
236+
return method;
237+
}();
240238

241239
return method;
242240
}

src/crypto/crypto_bio.h

-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ class NodeBIO : public MemoryRetainer {
182182
int eof_return_ = -1;
183183
Buffer* read_head_ = nullptr;
184184
Buffer* write_head_ = nullptr;
185-
186-
friend void node::crypto::InitCryptoOnce();
187185
};
188186

189187
} // namespace crypto

src/crypto/crypto_util.cc

-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ void InitCryptoOnce() {
184184
ERR_load_ENGINE_strings();
185185
ENGINE_load_builtin_engines();
186186
#endif // !OPENSSL_NO_ENGINE
187-
188-
NodeBIO::GetMethod();
189187
}
190188

191189
void GetFipsCrypto(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)