From 9cd0a1d58ca5c25480ff816b3ed13a5f439ccfc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sat, 21 Jan 2023 07:37:51 +0100 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 PR-URL: https://github.com/nodejs/node/pull/46250 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis --- src/crypto/crypto_keygen.cc | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/crypto/crypto_keygen.cc b/src/crypto/crypto_keygen.cc index 8def58a5f10cd9..752c8402aac2c1 100644 --- a/src/crypto/crypto_keygen.cc +++ b/src/crypto/crypto_keygen.cc @@ -16,7 +16,6 @@ using v8::Int32; using v8::Just; using v8::Local; using v8::Maybe; -using v8::Nothing; using v8::Object; using v8::Uint32; using v8::Value; @@ -63,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); }