Skip to content

Commit 7360333

Browse files
authoredFeb 17, 2022
Handle overflow in IXExponentialBackoff.cpp
1 parent 90f19e0 commit 7360333

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed
 

‎ixwebsocket/IXExponentialBackoff.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,27 @@ namespace ix
1414
uint32_t maxWaitBetweenReconnectionRetries,
1515
uint32_t minWaitBetweenReconnectionRetries)
1616
{
17-
uint32_t waitTime = (retryCount < 26) ? (std::pow(2, retryCount) * 100) : 0;
17+
// It's easy with a power function to go beyond 2^32, and then
18+
// have unexpected results, so prepare for that
19+
const uint32_t maxRetryCountWithoutOverflow = 26;
20+
21+
uint32_t waitTime = 0;
22+
if (retryCount < maxRetryCountWithoutOverflow)
23+
{
24+
waitTime = std::pow(2, retryCount) * 100;
25+
}
1826

1927
if (waitTime < minWaitBetweenReconnectionRetries)
2028
{
2129
waitTime = minWaitBetweenReconnectionRetries;
2230
}
2331

24-
if (waitTime > maxWaitBetweenReconnectionRetries || waitTime == 0)
32+
if (waitTime > maxWaitBetweenReconnectionRetries)
33+
{
34+
waitTime = maxWaitBetweenReconnectionRetries;
35+
}
36+
37+
if (retryCount >= maxRetryCountWithoutOverflow)
2538
{
2639
waitTime = maxWaitBetweenReconnectionRetries;
2740
}

0 commit comments

Comments
 (0)
Please sign in to comment.