Skip to content

Commit

Permalink
src: simplify ECDH::GetCurves()
Browse files Browse the repository at this point in the history
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: #44309
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
tniessen authored and juanarbol committed Oct 11, 2022
1 parent 5b5d95d commit 4c27d77
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/crypto/crypto_ec.cc
Expand Up @@ -104,23 +104,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)
Expand Down

0 comments on commit 4c27d77

Please sign in to comment.