Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
tniessen committed Aug 20, 2022
commit c88db1e173aa4eab81de513dde5033ee7d1f0d40
25 changes: 8 additions & 17 deletions src/crypto/crypto_ec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down