mirror of https://github.com/oxen-io/oxen-core.git
Rewrite logic to avoid unsigned int overflow
- When calculating the offset the calculation here is overflowing; since they are `uint64_t`s where overflow is well-defined that doesn't actually break anything (the overflowed value overflows in the other direction when added, and works as the intended subtraction), but it feels a bit icky. Rearranging the logic avoids it. - If given a small height (e.g. 3) the next payout height would come out as a large positive number (close to uint64_t max). The change above avoids this as well.
This commit is contained in:
parent
946b182384
commit
598c174c9c
|
@ -150,10 +150,11 @@ uint64_t account_public_address::modulus(uint64_t interval) const
|
|||
|
||||
uint64_t account_public_address::next_payout_height(uint64_t current_height, uint64_t interval) const
|
||||
{
|
||||
uint64_t next_payout_height = current_height + (modulus(interval) - current_height % interval);
|
||||
if (next_payout_height <= current_height)
|
||||
next_payout_height += interval;
|
||||
return next_payout_height;
|
||||
auto pay_offset = modulus(interval);
|
||||
auto curr_offset = current_height % interval;
|
||||
if (pay_offset <= curr_offset)
|
||||
pay_offset += interval;
|
||||
return current_height + pay_offset - curr_offset;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue