Skip to content

Commit

Permalink
src: replace unreachable code with static_assert
Browse files Browse the repository at this point in the history
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: nodejs#46209
  • Loading branch information
tniessen committed Jan 18, 2023
1 parent 671ffd7 commit 632c5b4
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/crypto/crypto_keygen.cc
Expand Up @@ -62,15 +62,11 @@ Maybe<bool> SecretKeyGenTraits::AdditionalConfig(
const FunctionCallbackInfo<Value>& args,
unsigned int* offset,
SecretKeyGenConfig* params) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[*offset]->IsUint32());
params->length = args[*offset].As<Uint32>()->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<uint64_t>(INT_MAX) * CHAR_BIT);
return Nothing<bool>();
}
uint32_t bits = args[*offset].As<Uint32>()->Value();
static_assert(std::numeric_limits<decltype(bits)>::max() / CHAR_BIT <=
INT_MAX);
params->length = bits / CHAR_BIT;
*offset += 1;
return Just(true);
}
Expand Down

0 comments on commit 632c5b4

Please sign in to comment.