fix: Safely handle nanosecond overflow in ClickCallback (#12686)
If you are creating a click callback using e.g. ChronoUnit.FOREVER.getDuration() this code will throw an ArithmeticException because toNanos overflows. The only way toNanos throws this exception is if the nanos overflow, so we can just safely cap it here as the max value for a long.
This commit is contained in:
parent
29fc853271
commit
4419afb9c9
1 changed files with 8 additions and 1 deletions
|
@ -65,8 +65,14 @@ public class ClickCallbackProviderImpl implements ClickCallback.Provider {
|
|||
private int remainingUses;
|
||||
|
||||
private StoredCallback(final @NotNull ClickCallback<Audience> callback, final ClickCallback.@NotNull Options options, final UUID id) {
|
||||
long lifetimeValue;
|
||||
this.callback = callback;
|
||||
this.lifetime = options.lifetime().toNanos();
|
||||
try {
|
||||
lifetimeValue = options.lifetime().toNanos();
|
||||
} catch (final ArithmeticException ex) {
|
||||
lifetimeValue = Long.MAX_VALUE;
|
||||
}
|
||||
this.lifetime = lifetimeValue;
|
||||
this.remainingUses = options.uses();
|
||||
this.id = id;
|
||||
}
|
||||
|
@ -82,6 +88,7 @@ public class ClickCallbackProviderImpl implements ClickCallback.Provider {
|
|||
}
|
||||
|
||||
public boolean expired() {
|
||||
if (this.lifetime == Long.MAX_VALUE) return false;
|
||||
return System.nanoTime() - this.startedAt >= this.lifetime;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue