Skip to content

Prevent infinite loops from zero-interval DatePeriods#499

Open
dereuromark wants to merge 1 commit into3.xfrom
fix-zero-interval-validation
Open

Prevent infinite loops from zero-interval DatePeriods#499
dereuromark wants to merge 1 commit into3.xfrom
fix-zero-interval-validation

Conversation

@dereuromark
Copy link
Member

Summary

  • Adds validation to ChronosPeriod and ChronosDatePeriod constructors to throw InvalidArgumentException when given a DatePeriod with a zero interval
  • Prevents infinite loops that crash the server when iterating over such periods

The Problem

Creating a ChronosPeriod with a zero interval causes an infinite loop:

$period = new DatePeriod(
    new DateTime('2025-01-01'),
    new DateInterval('PT0S'),  // Zero seconds!
    new DateTime('2025-01-02')
);

// This causes an infinite loop - the iterator never advances
foreach (new ChronosPeriod($period) as $date) {
    // Server crash...
}

The Fix

The constructor now validates the interval and throws an exception:

// Now throws InvalidArgumentException
new ChronosPeriod($period);
// "Cannot create a period with a zero interval. This would cause an infinite loop when iterating."

Related

Similar issue was reported for Carbon: briannesbitt/Carbon#3259

Adds validation to ChronosPeriod and ChronosDatePeriod constructors
to throw an InvalidArgumentException when given a DatePeriod with a
zero interval (e.g., PT0S or P0D).

Without this check, iterating over such a period causes an infinite
loop that can crash the server.

Also fixes typo in ChronosDatePeriodTest (ChronosDAte -> ChronosDate).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant