Skip to content

Commit

Permalink
src: simplify NodeBIO::GetMethod initialization
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
addaleax authored and danielleadams committed Jan 3, 2023
1 parent b88ee54 commit 8e1138d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 11 deletions.
12 changes: 5 additions & 7 deletions src/crypto/crypto_bio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,18 @@ 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);
BIO_meth_set_gets(method, Gets);
BIO_meth_set_ctrl(method, Ctrl);
BIO_meth_set_create(method, New);
BIO_meth_set_destroy(method, Free);
}
return method;
}();

return method;
}
Expand Down
2 changes: 0 additions & 2 deletions src/crypto/crypto_bio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/crypto/crypto_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ void InitCryptoOnce() {
ERR_load_ENGINE_strings();
ENGINE_load_builtin_engines();
#endif // !OPENSSL_NO_ENGINE

NodeBIO::GetMethod();
}

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

0 comments on commit 8e1138d

Please sign in to comment.