Skip to content

Commit 6b004f1

Browse files
authored
src,crypto: avoid tristate Maybe<bool> in ExportJWKEcKey()
The function currently uses the return value to convey whether an exception was thrown while it was running by using either Just(true) or Nothing<bool>(). Unfortunately, Maybe<bool> also has a third state - Just(false), which doesn't make any sense here. So this change avoids the possibility of a tristate return value by making use of Maybe<void> which only has two valid states - JustVoid() / Nothing<void>(), which fits right in. Signed-off-by: Darshan Sen <[email protected]> PR-URL: #42223 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 4d70dc7 commit 6b004f1

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/crypto/crypto_ec.cc

+8-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ using v8::FunctionCallbackInfo;
2424
using v8::FunctionTemplate;
2525
using v8::Int32;
2626
using v8::Just;
27+
using v8::JustVoid;
2728
using v8::Local;
2829
using v8::Maybe;
2930
using v8::Nothing;
@@ -711,7 +712,7 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport(
711712
}
712713
}
713714

714-
Maybe<bool> ExportJWKEcKey(
715+
Maybe<void> ExportJWKEcKey(
715716
Environment* env,
716717
std::shared_ptr<KeyObjectData> key,
717718
Local<Object> target) {
@@ -738,7 +739,7 @@ Maybe<bool> ExportJWKEcKey(
738739
env->context(),
739740
env->jwk_kty_string(),
740741
env->jwk_ec_string()).IsNothing()) {
741-
return Nothing<bool>();
742+
return Nothing<void>();
742743
}
743744

744745
if (SetEncodedValue(
@@ -753,7 +754,7 @@ Maybe<bool> ExportJWKEcKey(
753754
env->jwk_y_string(),
754755
y.get(),
755756
degree_bytes).IsNothing()) {
756-
return Nothing<bool>();
757+
return Nothing<void>();
757758
}
758759

759760
Local<String> crv_name;
@@ -774,14 +775,14 @@ Maybe<bool> ExportJWKEcKey(
774775
default: {
775776
THROW_ERR_CRYPTO_JWK_UNSUPPORTED_CURVE(
776777
env, "Unsupported JWK EC curve: %s.", OBJ_nid2sn(nid));
777-
return Nothing<bool>();
778+
return Nothing<void>();
778779
}
779780
}
780781
if (target->Set(
781782
env->context(),
782783
env->jwk_crv_string(),
783784
crv_name).IsNothing()) {
784-
return Nothing<bool>();
785+
return Nothing<void>();
785786
}
786787

787788
if (key->GetKeyType() == kKeyTypePrivate) {
@@ -791,10 +792,10 @@ Maybe<bool> ExportJWKEcKey(
791792
target,
792793
env->jwk_d_string(),
793794
pvt,
794-
degree_bytes);
795+
degree_bytes).IsJust() ? JustVoid() : Nothing<void>();
795796
}
796797

797-
return Just(true);
798+
return JustVoid();
798799
}
799800

800801
Maybe<bool> ExportJWKEdKey(

src/crypto/crypto_ec.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct ECKeyExportTraits final {
144144

145145
using ECKeyExportJob = KeyExportJob<ECKeyExportTraits>;
146146

147-
v8::Maybe<bool> ExportJWKEcKey(
147+
v8::Maybe<void> ExportJWKEcKey(
148148
Environment* env,
149149
std::shared_ptr<KeyObjectData> key,
150150
v8::Local<v8::Object> target);

src/crypto/crypto_keys.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ Maybe<bool> ExportJWKAsymmetricKey(
497497
break;
498498
}
499499
case EVP_PKEY_RSA: return ExportJWKRsaKey(env, key, target);
500-
case EVP_PKEY_EC: return ExportJWKEcKey(env, key, target);
500+
case EVP_PKEY_EC: return ExportJWKEcKey(env, key, target).IsJust() ?
501+
Just(true) : Nothing<bool>();
501502
case EVP_PKEY_ED25519:
502503
// Fall through
503504
case EVP_PKEY_ED448:

0 commit comments

Comments
 (0)