Skip to content

Commit 4c27d77

Browse files
tniessenjuanarbol
authored andcommitted
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: #44309 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 5b5d95d commit 4c27d77

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

src/crypto/crypto_ec.cc

+8-17
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,14 @@ void ECDH::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
104104
void ECDH::GetCurves(const FunctionCallbackInfo<Value>& args) {
105105
Environment* env = Environment::GetCurrent(args);
106106
const size_t num_curves = EC_get_builtin_curves(nullptr, 0);
107-
108-
if (num_curves) {
109-
std::vector<EC_builtin_curve> curves(num_curves);
110-
111-
if (EC_get_builtin_curves(curves.data(), num_curves)) {
112-
std::vector<Local<Value>> arr(num_curves);
113-
114-
for (size_t i = 0; i < num_curves; i++)
115-
arr[i] = OneByteString(env->isolate(), OBJ_nid2sn(curves[i].nid));
116-
117-
args.GetReturnValue().Set(
118-
Array::New(env->isolate(), arr.data(), arr.size()));
119-
return;
120-
}
121-
}
122-
123-
args.GetReturnValue().Set(Array::New(env->isolate()));
107+
std::vector<EC_builtin_curve> curves(num_curves);
108+
CHECK_EQ(EC_get_builtin_curves(curves.data(), num_curves), num_curves);
109+
110+
std::vector<Local<Value>> arr(num_curves);
111+
std::transform(curves.begin(), curves.end(), arr.begin(), [env](auto& curve) {
112+
return OneByteString(env->isolate(), OBJ_nid2sn(curve.nid));
113+
});
114+
args.GetReturnValue().Set(Array::New(env->isolate(), arr.data(), arr.size()));
124115
}
125116

126117
ECDH::ECDH(Environment* env, Local<Object> wrap, ECKeyPointer&& key)

0 commit comments

Comments
 (0)