From 04ec693a304c6bff31cf0538ade426179e60be94 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Thu, 22 Apr 2021 17:08:18 +0200 Subject: [PATCH] src: refactor to use THROW_* argument based snprintf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/38357 Reviewed-By: Tobias Nießen Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- src/crypto/crypto_cipher.cc | 19 ++++++++----------- src/crypto/crypto_dsa.cc | 4 +--- src/crypto/crypto_hash.cc | 4 +--- src/crypto/crypto_keys.cc | 4 +--- src/crypto/crypto_rsa.cc | 15 ++++++--------- src/crypto/crypto_scrypt.cc | 5 +---- 6 files changed, 18 insertions(+), 33 deletions(-) diff --git a/src/crypto/crypto_cipher.cc b/src/crypto/crypto_cipher.cc index b15795b691ea0b..5ce466582823ae 100644 --- a/src/crypto/crypto_cipher.cc +++ b/src/crypto/crypto_cipher.cc @@ -511,10 +511,10 @@ bool CipherBase::InitAuthenticated( if (mode == EVP_CIPH_GCM_MODE) { if (auth_tag_len != kNoAuthTagLength) { if (!IsValidGCMTagLength(auth_tag_len)) { - char msg[50]; - snprintf(msg, sizeof(msg), - "Invalid authentication tag length: %u", auth_tag_len); - THROW_ERR_CRYPTO_INVALID_AUTH_TAG(env(), msg); + THROW_ERR_CRYPTO_INVALID_AUTH_TAG( + env(), + "Invalid authentication tag length: %u", + auth_tag_len); return false; } @@ -523,9 +523,8 @@ bool CipherBase::InitAuthenticated( } } else { if (auth_tag_len == kNoAuthTagLength) { - char msg[128]; - snprintf(msg, sizeof(msg), "authTagLength required for %s", cipher_type); - THROW_ERR_CRYPTO_INVALID_AUTH_TAG(env(), msg); + THROW_ERR_CRYPTO_INVALID_AUTH_TAG( + env(), "authTagLength required for %s", cipher_type); return false; } @@ -633,10 +632,8 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo& args) { } if (!is_valid) { - char msg[50]; - snprintf(msg, sizeof(msg), - "Invalid authentication tag length: %u", tag_len); - return THROW_ERR_CRYPTO_INVALID_AUTH_TAG(env, msg); + return THROW_ERR_CRYPTO_INVALID_AUTH_TAG( + env, "Invalid authentication tag length: %u", tag_len); } cipher->auth_tag_len_ = tag_len; diff --git a/src/crypto/crypto_dsa.cc b/src/crypto/crypto_dsa.cc index 6ee8cf7e18c02e..271db427fa8539 100644 --- a/src/crypto/crypto_dsa.cc +++ b/src/crypto/crypto_dsa.cc @@ -84,9 +84,7 @@ Maybe DsaKeyGenTraits::AdditionalConfig( params->params.modulus_bits = args[*offset].As()->Value(); params->params.divisor_bits = args[*offset + 1].As()->Value(); if (params->params.divisor_bits < -1) { - char msg[1024]; - snprintf(msg, sizeof(msg), "invalid value for divisor_bits"); - THROW_ERR_OUT_OF_RANGE(env, msg); + THROW_ERR_OUT_OF_RANGE(env, "invalid value for divisor_bits"); return Nothing(); } diff --git a/src/crypto/crypto_hash.cc b/src/crypto/crypto_hash.cc index 664ffb847215af..ba2c52268a23ff 100644 --- a/src/crypto/crypto_hash.cc +++ b/src/crypto/crypto_hash.cc @@ -233,9 +233,7 @@ Maybe HashTraits::AdditionalConfig( Utf8Value digest(env->isolate(), args[offset]); params->digest = EVP_get_digestbyname(*digest); if (UNLIKELY(params->digest == nullptr)) { - char msg[1024]; - snprintf(msg, sizeof(msg), "Invalid digest: %s", *digest); - THROW_ERR_CRYPTO_INVALID_DIGEST(env); + THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *digest); return Nothing(); } diff --git a/src/crypto/crypto_keys.cc b/src/crypto/crypto_keys.cc index be12022bdb7f1d..21cc988cee6a18 100644 --- a/src/crypto/crypto_keys.cc +++ b/src/crypto/crypto_keys.cc @@ -515,9 +515,7 @@ std::shared_ptr ImportJWKAsymmetricKey( return ImportJWKEcKey(env, jwk, args, offset); } - char msg[1024]; - snprintf(msg, sizeof(msg), "%s is not a supported JWK key type", kty); - THROW_ERR_CRYPTO_INVALID_JWK(env, msg); + THROW_ERR_CRYPTO_INVALID_JWK(env, "%s is not a supported JWK key type", kty); return std::shared_ptr(); } diff --git a/src/crypto/crypto_rsa.cc b/src/crypto/crypto_rsa.cc index a77ded918ebdda..5fa91cce1a6ad2 100644 --- a/src/crypto/crypto_rsa.cc +++ b/src/crypto/crypto_rsa.cc @@ -138,9 +138,7 @@ Maybe RsaKeyGenTraits::AdditionalConfig( Utf8Value digest(env->isolate(), args[*offset]); params->params.md = EVP_get_digestbyname(*digest); if (params->params.md == nullptr) { - char msg[1024]; - snprintf(msg, sizeof(msg), "md specifies an invalid digest"); - THROW_ERR_CRYPTO_INVALID_DIGEST(env, msg); + THROW_ERR_CRYPTO_INVALID_DIGEST(env, "md specifies an invalid digest"); return Nothing(); } } @@ -150,9 +148,8 @@ Maybe RsaKeyGenTraits::AdditionalConfig( Utf8Value digest(env->isolate(), args[*offset + 1]); params->params.mgf1_md = EVP_get_digestbyname(*digest); if (params->params.mgf1_md == nullptr) { - char msg[1024]; - snprintf(msg, sizeof(msg), "mgf1_md specifies an invalid digest"); - THROW_ERR_CRYPTO_INVALID_DIGEST(env, msg); + THROW_ERR_CRYPTO_INVALID_DIGEST(env, + "mgf1_md specifies an invalid digest"); return Nothing(); } } @@ -161,9 +158,9 @@ Maybe RsaKeyGenTraits::AdditionalConfig( CHECK(args[*offset + 2]->IsInt32()); params->params.saltlen = args[*offset + 2].As()->Value(); if (params->params.saltlen < 0) { - char msg[1024]; - snprintf(msg, sizeof(msg), "salt length is out of range"); - THROW_ERR_OUT_OF_RANGE(env, msg); + THROW_ERR_OUT_OF_RANGE( + env, + "salt length is out of range"); return Nothing(); } } diff --git a/src/crypto/crypto_scrypt.cc b/src/crypto/crypto_scrypt.cc index 39d6b3fd0d8d6a..077c26554b2f1f 100644 --- a/src/crypto/crypto_scrypt.cc +++ b/src/crypto/crypto_scrypt.cc @@ -111,9 +111,7 @@ Maybe ScryptTraits::AdditionalConfig( params->length = args[offset + 6].As()->Value(); if (params->length < 0) { - char msg[1024]; - snprintf(msg, sizeof(msg), "length must be <= %d", INT_MAX); - THROW_ERR_OUT_OF_RANGE(env, msg); + THROW_ERR_OUT_OF_RANGE(env, "length must be <= %d", INT_MAX); return Nothing(); } @@ -151,4 +149,3 @@ bool ScryptTraits::DeriveBits( } // namespace crypto } // namespace node -