Repository navigation
fix: validate the amount in Interval.range (zero/negative hang or run away) - #1090
Open
rahul05ranjan wants to merge 1 commit into
Open
rahul05ranjan wants to merge 1 commit into
rahul05ranjan wants to merge 1 commit into
Conversation
A zero amount made range() yield the same start value forever: the
advance step add(**{unit: i}) with i == 0 never moves, so the loop
condition start <= end never turns false and any consuming for-loop
hangs. A negative amount runs away from the stated end in the opposite
direction, unbounded, for the same missing-validation reason.
Reject non-positive amounts with ValueError, matching the documented
direction handling (the direction itself is applied via add/subtract
and le/ge, so the step magnitude must be positive in both directions).
Fixes python-pendulum#1080
Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Interval.range(unit, amount)accepts any integeramount, including0— which produces a generator that yields the same starting value forever and never reaches its stated end:The loop's advance step (
add(**{unit: i})withistuck at 0) and its termination check (start <= end) both depend onamount, so any caller that consumes the generator without an independent bound — a plainforloop,list(...)— hangs at full CPU with no way to recover. A caller that builds the step from user/config input can be made to hang a worker indefinitely.A related case, a wrong-sign
amount(e.g.-1on a forward interval), does not hang but runs away unbounded in the opposite direction from the stated end — the same missing validation.Fix: reject non-positive amounts with
ValueError. The direction is already applied internally (add/levssubtract/geper the interval's orientation), so the step magnitude must be positive in both directions.Fixes #1080.
Verification (system Python 3.13, wheels on the path, nothing installed):
master@e06dace:amount=0repeats the start indefinitely (islice of an infinite generator),amount=-1yields 2026-01-01, 2025-12-31, 2025-12-30 … running backward, never reaching the end.tests/interval/test_range.py(zero and negative amount →ValueError): both fail with the infinite/escaping behavior on unpatchedmaster, pass with this change.tests/interval,tests/datetime,tests/parsing,tests/formatting,tests/date,tests/time: 1104 passed, zero regressions.