Skip to content

Commit

Permalink
Handle a possible overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
jowlyzhang committed Oct 1, 2024
1 parent 389e66b commit b52e92c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions db/version_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1968,9 +1968,16 @@ uint64_t VersionStorageInfo::GetEstimatedActiveKeys() const {
}

if (current_num_samples_ < file_count) {
// casting to avoid overflowing
return static_cast<uint64_t>(
(est * static_cast<double>(file_count) / current_num_samples_));
assert(current_num_samples_ != 0);
assert(est != 0);
double multiplier = static_cast<double>(file_count) / current_num_samples_;
double maximum_multiplier =
static_cast<double>(std::numeric_limits<uint64_t>::max()) / est;
// If it can overflow, we return the maximum unsigned long.
if (multiplier >= maximum_multiplier) {
return std::numeric_limits<uint64_t>::max();
}
return static_cast<uint64_t>(est * multiplier);
} else {
return est;
}
Expand Down

0 comments on commit b52e92c

Please sign in to comment.