Skip to content

Commit

Permalink
src,crypto: handle empty maybe correctly in crypto_dh.cc
Browse files Browse the repository at this point in the history
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 <raisinten@gmail.com>

PR-URL: nodejs#42492
Refs: nodejs#39941
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
RaisinTen authored and xtx1130 committed Apr 25, 2022
1 parent b59678a commit 7845769
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ using v8::ReadOnly;
using v8::SideEffectType;
using v8::Signature;
using v8::String;
using v8::Uint8Array;
using v8::Value;

namespace crypto {
Expand Down Expand Up @@ -637,8 +636,10 @@ void DiffieHellman::Stateless(const FunctionCallbackInfo<Value>& args) {
ManagedEVPPKey our_key = our_key_object->Data()->GetAsymmetricKey();
ManagedEVPPKey their_key = their_key_object->Data()->GetAsymmetricKey();

Local<Value> out = StatelessDiffieHellmanThreadsafe(our_key, their_key)
.ToBuffer(env).FromMaybe(Local<Uint8Array>());
Local<Value> 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");
Expand Down

0 comments on commit 7845769

Please sign in to comment.