Skip to content

Commit

Permalink
src: avoid casting std::trunc(... / ...) to size_t
Browse files Browse the repository at this point in the history
Given that the divisor is not zero, the result of dividing unsigned
integers is an unsigned integer that is always rounded down, i.e.,
there is no need to call std::trunc(). Doing so unnecessarily yields
a floating-point number, requiring the result to be cast to an unsigned
integer again.

PR-URL: #44467
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
tniessen authored and RafaelGSS committed Sep 7, 2022
1 parent 4331bbe commit 18b7208
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions src/crypto/crypto_keygen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ Maybe<bool> SecretKeyGenTraits::AdditionalConfig(
SecretKeyGenConfig* params) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[*offset]->IsUint32());
params->length = static_cast<size_t>(
std::trunc(args[*offset].As<Uint32>()->Value() / CHAR_BIT));
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",
Expand Down

0 comments on commit 18b7208

Please sign in to comment.