Skip to content

Commit c190189

Browse files
danbevaddaleax
authored andcommitted
src: add encoding_type variable in WritePrivateKey
This commit adds a local variable named encoding_type which is set to the value of the Maybe using ToChecked(). The motivation for this is the code for ToChecked() could be executed multiple times depending on path taken at runtime. I also think this improves readability, or at least it is as readable as before this change. PR-URL: #34181 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent 32c51c6 commit c190189

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/node_crypto.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -3128,7 +3128,8 @@ static MaybeLocal<Value> WritePrivateKey(
31283128

31293129
bool err;
31303130

3131-
if (config.type_.ToChecked() == kKeyEncodingPKCS1) {
3131+
PKEncodingType encoding_type = config.type_.ToChecked();
3132+
if (encoding_type == kKeyEncodingPKCS1) {
31323133
// PKCS#1 is only permitted for RSA keys.
31333134
CHECK_EQ(EVP_PKEY_id(pkey), EVP_PKEY_RSA);
31343135

@@ -3148,7 +3149,7 @@ static MaybeLocal<Value> WritePrivateKey(
31483149
CHECK_NULL(config.cipher_);
31493150
err = i2d_RSAPrivateKey_bio(bio.get(), rsa.get()) != 1;
31503151
}
3151-
} else if (config.type_.ToChecked() == kKeyEncodingPKCS8) {
3152+
} else if (encoding_type == kKeyEncodingPKCS8) {
31523153
if (config.format_ == kKeyFormatPEM) {
31533154
// Encode PKCS#8 as PEM.
31543155
err = PEM_write_bio_PKCS8PrivateKey(
@@ -3168,7 +3169,7 @@ static MaybeLocal<Value> WritePrivateKey(
31683169
nullptr, nullptr) != 1;
31693170
}
31703171
} else {
3171-
CHECK_EQ(config.type_.ToChecked(), kKeyEncodingSEC1);
3172+
CHECK_EQ(encoding_type, kKeyEncodingSEC1);
31723173

31733174
// SEC1 is only permitted for EC keys.
31743175
CHECK_EQ(EVP_PKEY_id(pkey), EVP_PKEY_EC);

0 commit comments

Comments
 (0)