diff options
author | Tobias Nießen <tniessen@tnie.de> | 2022-08-26 22:59:51 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-26 22:59:51 +0300 |
commit | 51816005262597c2b281aea7931b30000ca226a0 (patch) | |
tree | 6641e9f320ba36b9bbf2dca63fcf8ab02783966e /src | |
parent | f59d8d3dc3247c60b20183d56ec995064bda67da (diff) |
src: simplify ECDH::GetCurves()
There is no need to explicitly branch based on num_curves or on the
return value of the second call to EC_get_builtin_curves. Remove
unnecessary branches and replace the loop with a functional transform.
PR-URL: https://github.com/nodejs/node/pull/44309
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src')
-rw-r--r-- | src/crypto/crypto_ec.cc | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index 0c14b09f638..2fc759e452f 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -109,23 +109,14 @@ void ECDH::RegisterExternalReferences(ExternalReferenceRegistry* registry) { void ECDH::GetCurves(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); const size_t num_curves = EC_get_builtin_curves(nullptr, 0); - - if (num_curves) { - std::vector<EC_builtin_curve> curves(num_curves); - - if (EC_get_builtin_curves(curves.data(), num_curves)) { - std::vector<Local<Value>> arr(num_curves); - - for (size_t i = 0; i < num_curves; i++) - arr[i] = OneByteString(env->isolate(), OBJ_nid2sn(curves[i].nid)); - - args.GetReturnValue().Set( - Array::New(env->isolate(), arr.data(), arr.size())); - return; - } - } - - args.GetReturnValue().Set(Array::New(env->isolate())); + std::vector<EC_builtin_curve> curves(num_curves); + CHECK_EQ(EC_get_builtin_curves(curves.data(), num_curves), num_curves); + + std::vector<Local<Value>> arr(num_curves); + std::transform(curves.begin(), curves.end(), arr.begin(), [env](auto& curve) { + return OneByteString(env->isolate(), OBJ_nid2sn(curve.nid)); + }); + args.GetReturnValue().Set(Array::New(env->isolate(), arr.data(), arr.size())); } ECDH::ECDH(Environment* env, Local<Object> wrap, ECKeyPointer&& key) |