Merge pull request #8227 from sfc-gh-mpilman/bugfixes/ubsan-ratecontrol

prevent int overflow
This commit is contained in:
Markus Pilman 2022-09-19 17:28:00 -06:00 committed by GitHub
commit f471f3da04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 1 deletions

View File

@ -53,7 +53,13 @@ public:
// Replenish budget based on time since last update
double ts = now();
// returnUnused happens to do exactly what we want here
returnUnused((ts - m_last_update) / m_seconds * m_limit);
auto unused = double(m_limit) * (ts - m_last_update) / m_seconds;
if (unused >= double(std::numeric_limits<int>::max())) {
// prevent int overflow
m_budget = m_limit;
} else {
returnUnused(int(unused));
}
m_last_update = ts;
m_budget -= n;
// If budget is still >= 0 then it's safe to use the allowance right now.