Skip to content

Commit 5181600

Browse files
authored
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 f59d8d3 commit 5181600

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

src/crypto/crypto_ec.cc

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,14 @@ void ECDH::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
109109
void ECDH::GetCurves(const FunctionCallbackInfo<Value>& args) {
110110
Environment* env = Environment::GetCurrent(args);
111111
const size_t num_curves = EC_get_builtin_curves(nullptr, 0);
112-
113-
if (num_curves) {
114-
std::vector<EC_builtin_curve> curves(num_curves);
115-
116-
if (EC_get_builtin_curves(curves.data(), num_curves)) {
117-
std::vector<Local<Value>> arr(num_curves);
118-
119-
for (size_t i = 0; i < num_curves; i++)
120-
arr[i] = OneByteString(env->isolate(), OBJ_nid2sn(curves[i].nid));
121-
122-
args.GetReturnValue().Set(
123-
Array::New(env->isolate(), arr.data(), arr.size()));
124-
return;
125-
}
126-
}
127-
128-
args.GetReturnValue().Set(Array::New(env->isolate()));
112+
std::vector<EC_builtin_curve> curves(num_curves);
113+
CHECK_EQ(EC_get_builtin_curves(curves.data(), num_curves), num_curves);
114+
115+
std::vector<Local<Value>> arr(num_curves);
116+
std::transform(curves.begin(), curves.end(), arr.begin(), [env](auto& curve) {
117+
return OneByteString(env->isolate(), OBJ_nid2sn(curve.nid));
118+
});
119+
args.GetReturnValue().Set(Array::New(env->isolate(), arr.data(), arr.size()));
129120
}
130121

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

0 commit comments

Comments
 (0)