From c1901896b7befc10c22e161cb223c8282f2e60f9 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Fri, 3 Jul 2020 13:24:11 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/34181 Reviewed-By: Anna Henningsen Reviewed-By: David Carlier --- src/node_crypto.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 4eee56be11034a..a9d52a98ec238c 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3128,7 +3128,8 @@ static MaybeLocal WritePrivateKey( bool err; - if (config.type_.ToChecked() == kKeyEncodingPKCS1) { + PKEncodingType encoding_type = config.type_.ToChecked(); + if (encoding_type == kKeyEncodingPKCS1) { // PKCS#1 is only permitted for RSA keys. CHECK_EQ(EVP_PKEY_id(pkey), EVP_PKEY_RSA); @@ -3148,7 +3149,7 @@ static MaybeLocal WritePrivateKey( CHECK_NULL(config.cipher_); err = i2d_RSAPrivateKey_bio(bio.get(), rsa.get()) != 1; } - } else if (config.type_.ToChecked() == kKeyEncodingPKCS8) { + } else if (encoding_type == kKeyEncodingPKCS8) { if (config.format_ == kKeyFormatPEM) { // Encode PKCS#8 as PEM. err = PEM_write_bio_PKCS8PrivateKey( @@ -3168,7 +3169,7 @@ static MaybeLocal WritePrivateKey( nullptr, nullptr) != 1; } } else { - CHECK_EQ(config.type_.ToChecked(), kKeyEncodingSEC1); + CHECK_EQ(encoding_type, kKeyEncodingSEC1); // SEC1 is only permitted for EC keys. CHECK_EQ(EVP_PKEY_id(pkey), EVP_PKEY_EC);