Skip to content

Commit

Permalink
Merge pull request #12676 from uckelman/increment_wrap_around
Browse files Browse the repository at this point in the history
IncrementPropertly: Corrected bad wrap-around calculation when increment is negative
  • Loading branch information
uckelman authored Sep 10, 2023
2 parents a0a331e + 87333ac commit 53ccb7e
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ public String getNewValue(String oldValue) {
else if (constraints.isWrap()) {
final int min = constraints.getMinimumValue();
final int max = constraints.getMaximumValue();
value = min + ((value - min + incr) % (max - min + incr));
final int range = max - min + 1;
// NB: value - min + incr could be < 0 (but > -range), which is
// why we add range and mod a second time to ensure that the
// result is in [0,range).
value = min + ((value - min + incr) % range + range) % range;
}
else {
value += incr;
Expand Down

0 comments on commit 53ccb7e

Please sign in to comment.