From 441ea31fcad7efa9c8921704c49281bb8f7d1ef1 Mon Sep 17 00:00:00 2001 From: Markus Pilman Date: Mon, 19 Sep 2022 14:01:12 -0600 Subject: [PATCH] prevent int overflow --- flow/include/flow/IRateControl.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/flow/include/flow/IRateControl.h b/flow/include/flow/IRateControl.h index 28a2408537..2cfc066c1f 100644 --- a/flow/include/flow/IRateControl.h +++ b/flow/include/flow/IRateControl.h @@ -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::max())) { + // prevent int overflow + m_budget = m_limit; + } else { + returnUnused(int(std::min(unused, double(m_limit)))); + } m_last_update = ts; m_budget -= n; // If budget is still >= 0 then it's safe to use the allowance right now.