From 4c27d77e5c5f19bc009dd0ba7072a2fc7856c337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Fri, 26 Aug 2022 21:59:51 +0200 Subject: [PATCH] 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 Reviewed-By: Anna Henningsen --- src/crypto/crypto_ec.cc | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index e9fe65ffd571ca..d3840d77ff1660 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -104,23 +104,14 @@ void ECDH::RegisterExternalReferences(ExternalReferenceRegistry* registry) { void ECDH::GetCurves(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); const size_t num_curves = EC_get_builtin_curves(nullptr, 0); - - if (num_curves) { - std::vector curves(num_curves); - - if (EC_get_builtin_curves(curves.data(), num_curves)) { - std::vector> 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 curves(num_curves); + CHECK_EQ(EC_get_builtin_curves(curves.data(), num_curves), num_curves); + + std::vector> 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 wrap, ECKeyPointer&& key)