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: #46209
PR-URL: #46250
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
tniessen authored and ruyadorno committed Jan 31, 2023
1 parent 312e10c commit 8307a4b
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/crypto/crypto_keygen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,15 +61,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 8307a4b

Please sign in to comment.