Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Timespan Addition Overflow Exception #6328

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.Utils;
import ch.njol.util.Math2;
import org.bukkit.util.Vector;
import org.skriptlang.skript.lang.arithmetic.Arithmetics;
import org.skriptlang.skript.lang.arithmetic.Operator;
Expand Down Expand Up @@ -84,7 +85,7 @@ public class DefaultOperations {
});

// Timespan - Timespan
Arithmetics.registerOperation(Operator.ADDITION, Timespan.class, (left, right) -> new Timespan(left.getMilliSeconds() + right.getMilliSeconds()));
Arithmetics.registerOperation(Operator.ADDITION, Timespan.class, (left, right) -> new Timespan(Math2.addClamped(left.getMilliSeconds(), right.getMilliSeconds())));
Arithmetics.registerOperation(Operator.SUBTRACTION, Timespan.class, (left, right) -> new Timespan(Math.max(0, left.getMilliSeconds() - right.getMilliSeconds())));
Arithmetics.registerDifference(Timespan.class, (left, right) -> new Timespan(Math.abs(left.getMilliSeconds() - right.getMilliSeconds())));
Arithmetics.registerDefaultValue(Timespan.class, Timespan::new);
Expand All @@ -95,7 +96,7 @@ public class DefaultOperations {
long scalar = right.longValue();
if (scalar < 0)
return null;
return new Timespan(left.getMilliSeconds() * scalar);
return new Timespan(Math2.multiplyClamped(left.getMilliSeconds(), scalar));
}, (left, right) -> {
long scalar = left.longValue();
if (scalar < 0)
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/ch/njol/util/Math2.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ public static long round(double value) {
public static float safe(float value) {
return Float.isFinite(value) ? value : 0;
}

/**
* @param x the first value
* @param y the second value
* @return the sum of x and y, or {@link Long#MAX_VALUE} in case of an overflow
*/
public static long addClamped(long x, long y) {
long result = x + y;
// Logic extracted from Math#addExact to avoid having to catch an expensive exception
boolean causedOverflow = ((x ^ result) & (y ^ result)) < 0;
if (causedOverflow)
return Long.MAX_VALUE;
return result;
}

public static long multiplyClamped(long x, long y) {
long result = x * y;
long ax = Math.abs(x);
long ay = Math.abs(y);
// Logic extracted from Math#multiplyExact to avoid having to catch an expensive exception
if (((ax | ay) >>> 31 != 0) && (((y != 0) && (result / y != x)) || (x == Long.MIN_VALUE && y == -1)))
// If either x or y is negative return the min value, otherwise return the max value
return x < 0 == y < 0 ? Long.MAX_VALUE : Long.MIN_VALUE;
return result;
}

@Deprecated
@ScheduledForRemoval
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test "timespan overflow exception":
assert 1000000000 years + 10000000 years is set with "timespan addition overflow did not return max value"
assert 1000000000 years * 10000000 is set with "timespan addition overflow did not return max value"
Loading