From b759cbb057ce6631d97221a239a4a8865180123a Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 27 Mar 2022 18:20:20 +0530 Subject: [PATCH] src,crypto: handle empty maybe correctly in crypto_dh.cc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Buffer::Length() dereferences the passed Local, so calling it when the underlying pointer is a nullptr would lead to a crash. This fixes that by returning early instead. Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/42492 Refs: https://github.com/nodejs/node/pull/39941 Reviewed-By: Tobias Nießen Reviewed-By: Anna Henningsen --- src/crypto/crypto_dh.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/crypto/crypto_dh.cc b/src/crypto/crypto_dh.cc index 44269dbe482d89..590d4c8d67ca86 100644 --- a/src/crypto/crypto_dh.cc +++ b/src/crypto/crypto_dh.cc @@ -30,7 +30,6 @@ using v8::ReadOnly; using v8::SideEffectType; using v8::Signature; using v8::String; -using v8::Uint8Array; using v8::Value; namespace crypto { @@ -631,8 +630,10 @@ void DiffieHellman::Stateless(const FunctionCallbackInfo& args) { ManagedEVPPKey our_key = our_key_object->Data()->GetAsymmetricKey(); ManagedEVPPKey their_key = their_key_object->Data()->GetAsymmetricKey(); - Local out = StatelessDiffieHellmanThreadsafe(our_key, their_key) - .ToBuffer(env).FromMaybe(Local()); + Local out; + if (!StatelessDiffieHellmanThreadsafe(our_key, their_key) + .ToBuffer(env) + .ToLocal(&out)) return; if (Buffer::Length(out) == 0) return ThrowCryptoError(env, ERR_get_error(), "diffieHellman failed");