summaryrefslogtreecommitdiff
path: root/src/common/util.c
diff options
context:
space:
mode:
authorteor <teor2345@gmail.com>2014-12-25 20:52:10 +1100
committerteor <teor2345@gmail.com>2015-05-06 18:05:15 +1000
commit09cac24373e5a13cc527bf2f32132a9479d4ae1e (patch)
tree68c859998e732983d6e3524817dde86e5b3942f8 /src/common/util.c
parent6d54bdbdcf076167c1b73bfb5bef9fd1c3921796 (diff)
downloadtor-09cac24373e5a13cc527bf2f32132a9479d4ae1e.tar.gz
tor-09cac24373e5a13cc527bf2f32132a9479d4ae1e.zip
Handle edge cases in the round_*_to_next_multiple_of functions
Consistently check for overflow in round_*_to_next_multiple_of. Check all round_*_to_next_multiple_of functions with expected values. Check all round_*_to_next_multiple_of functions with maximal values. Related to HS stats in #13192.
Diffstat (limited to 'src/common/util.c')
-rw-r--r--src/common/util.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 52b3e04946..3e680d2d9e 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -491,7 +491,9 @@ round_to_power_of_2(uint64_t u64)
unsigned
round_to_next_multiple_of(unsigned number, unsigned divisor)
{
- number += divisor - 1;
+ tor_assert(divisor > 0);
+ if (UINT_MAX - divisor + 1 >= number)
+ number += divisor - 1;
number -= number % divisor;
return number;
}
@@ -501,7 +503,9 @@ round_to_next_multiple_of(unsigned number, unsigned divisor)
uint32_t
round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor)
{
- number += divisor - 1;
+ tor_assert(divisor > 0);
+ if (UINT32_MAX - divisor + 1 >= number)
+ number += divisor - 1;
number -= number % divisor;
return number;
}
@@ -511,7 +515,9 @@ round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor)
uint64_t
round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor)
{
- number += divisor - 1;
+ tor_assert(divisor > 0);
+ if (UINT64_MAX - divisor + 1 >= number)
+ number += divisor - 1;
number -= number % divisor;
return number;
}