Skip to content

Add isFinite check on Duration.fromDurationLike(<numeric>) #1711

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

Merged
merged 3 commits into from
Jun 12, 2025

Conversation

dobon
Copy link
Contributor

@dobon dobon commented Jun 11, 2025

Bug fix for #1118

This patch addresses a bug where Interval.after(new Date(0), Infinity) fails to throw an error, and the Infinity is interpreted as 0.

I'd say the bug here is that we're returning a "valid" Interval object. Somewhere in there the infinity is being treated as effectively a zero. --icambron, in the issue thread

The underlying problem is that Interval.after and Interval.before create a Duration from their second argument using the Duration.fromDurationLike method, and the fromDurationLike method has different behavior based on whether its argument is numeric, a luxon Duration, or an object. The bug is that we were checking isNumber, but not checking isFinite, which allows invalid numerics (ie: Infinity and NaN).

So in this patch I've added the necessary isFinite check and unit tests for fromDurationLike(Infinity) and fromDurationLike(NaN).

@diesieben07
Copy link
Collaborator

I think a better place to fix this would be asNumber, which is where we're already checking for NaN. That has the effect of also disallowing infinities in other places, such as DateTime.fromObject.

@diesieben07 diesieben07 merged commit ddd1ab4 into moment:master Jun 12, 2025
4 checks passed
@diesieben07
Copy link
Collaborator

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants