From 632c5b4f4ad99f96e8bde8647e06ca7d05605044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Wed, 18 Jan 2023 12:18:07 +0000 Subject: [PATCH] src: replace unreachable code with static_assert This function divides an unsigned 32-bit integer by 8, effectively right-shifting it by three bits, so the result must be less than INT_MAX. Refs: https://github.com/nodejs/node/pull/46209 --- src/crypto/crypto_keygen.cc | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/crypto/crypto_keygen.cc b/src/crypto/crypto_keygen.cc index cf785244467fa8..954a9ca703d827 100644 --- a/src/crypto/crypto_keygen.cc +++ b/src/crypto/crypto_keygen.cc @@ -62,15 +62,11 @@ Maybe SecretKeyGenTraits::AdditionalConfig( const FunctionCallbackInfo& args, unsigned int* offset, SecretKeyGenConfig* params) { - Environment* env = Environment::GetCurrent(args); CHECK(args[*offset]->IsUint32()); - params->length = args[*offset].As()->Value() / CHAR_BIT; - if (params->length > INT_MAX) { - THROW_ERR_OUT_OF_RANGE(env, - "length must be less than or equal to %u bits", - static_cast(INT_MAX) * CHAR_BIT); - return Nothing(); - } + uint32_t bits = args[*offset].As()->Value(); + static_assert(std::numeric_limits::max() / CHAR_BIT <= + INT_MAX); + params->length = bits / CHAR_BIT; *offset += 1; return Just(true); }