Skip to content

Commit

Permalink
Merge pull request #67 from j0k3r/fix-infinity-loop
Browse files Browse the repository at this point in the history
Avoid infinity loop & division by zero
  • Loading branch information
mtdowling committed Dec 19, 2014
2 parents 521a949 + 99e934e commit 99d5db9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ php:
- 5.5
- 5.6
- hhvm

matrix:
allow_failures:
- php: hhvm
Expand Down
6 changes: 5 additions & 1 deletion src/Cron/AbstractField.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function isInIncrementsOfRanges($dateValue, $value)
{
$parts = array_map('trim', explode('/', $value, 2));
$stepSize = isset($parts[1]) ? $parts[1] : 0;
if ($parts[0] == '*' || $parts[0] === '0') {
if (($parts[0] == '*' || $parts[0] === '0') && 0 !== $stepSize) {
return (int) $dateValue % $stepSize == 0;
}

Expand All @@ -89,6 +89,10 @@ public function isInIncrementsOfRanges($dateValue, $value)
return false;
}

if ($dateValue > $offset && 0 === $stepSize) {
return false;
}

for ($i = $offset; $i <= $to; $i+= $stepSize) {
if ($i == $dateValue) {
return true;
Expand Down
54 changes: 27 additions & 27 deletions src/Cron/CronExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class CronExpression
* Factory method to create a new CronExpression.
*
* @param string $expression The CRON expression to create. There are
* several special predefined values which can be used to substitute the
* CRON expression:
* several special predefined values which can be used to substitute the
* CRON expression:
*
* `@yearly`, `@annually` - Run once a year, midnight, Jan. 1 - 0 0 1 1 *
* `@monthly` - Run once a month, midnight, first of month - 0 0 1 * *
Expand Down Expand Up @@ -133,15 +133,15 @@ public function setPart($position, $value)
/**
* Get a next run date relative to the current date or a specific date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning a
* matching next run date. 0, the default, will return the current
* date and time if the next run date falls on the current date and
* time. Setting this value to 1 will skip the first match and go to
* the second match. Setting this value to 2 will skip the first 2
* matches and so on.
* @param bool $allowCurrentDate Set to TRUE to return the current date if
* it matches the cron expression.
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning a
* matching next run date. 0, the default, will return the current
* date and time if the next run date falls on the current date and
* time. Setting this value to 1 will skip the first match and go to
* the second match. Setting this value to 2 will skip the first 2
* matches and so on.
* @param bool $allowCurrentDate Set to TRUE to return the current date if
* it matches the cron expression.
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
Expand All @@ -154,10 +154,10 @@ public function getNextRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate
/**
* Get a previous run date relative to the current date or a specific date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
Expand All @@ -171,11 +171,11 @@ public function getPreviousRunDate($currentTime = 'now', $nth = 0, $allowCurrent
/**
* Get multiple run dates starting at the current date or a specific date
*
* @param int $total Set the total number of dates to calculate
* @param string|\DateTime $currentTime Relative calculation date
* @param bool $invert Set to TRUE to retrieve previous dates
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param int $total Set the total number of dates to calculate
* @param string|\DateTime $currentTime Relative calculation date
* @param bool $invert Set to TRUE to retrieve previous dates
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
*
* @return array Returns an array of run dates
*/
Expand All @@ -193,10 +193,10 @@ public function getMultipleRunDates($total, $currentTime = 'now', $invert = fals
* Get all or part of the CRON expression
*
* @param string $part Specify the part to retrieve or NULL to get the full
* cron schedule string.
* cron schedule string.
*
* @return string|null Returns the CRON expression, a part of the
* CRON expression, or NULL if the part was specified but not found
* CRON expression, or NULL if the part was specified but not found
*/
public function getExpression($part = null)
{
Expand Down Expand Up @@ -252,11 +252,11 @@ public function isDue($currentTime = 'now')
/**
* Get the next or previous run date of the expression relative to a date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $invert Set to TRUE to go backwards in time
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $invert Set to TRUE to go backwards in time
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
Expand Down
1 change: 1 addition & 0 deletions src/Cron/DayOfWeekField.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function increment(\DateTime $date, $invert = false)
public function validate($value)
{
$value = $this->convertLiterals($value);

return (bool) preg_match('/^(\*|[0-7](L?|#[1-5]))([\/\,\-][0-7]+)*$/', $value);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Cron/AbstractFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function testTestsIfInIncrementsOfRanges()
$this->assertFalse($f->isInIncrementsOfRanges(2, '3-59/13'));
$this->assertFalse($f->isInIncrementsOfRanges(14, '*/13'));
$this->assertFalse($f->isInIncrementsOfRanges(14, '3-59/2'));
$this->assertFalse($f->isInIncrementsOfRanges(3, '2-59'));
$this->assertFalse($f->isInIncrementsOfRanges(3, '2'));
$this->assertFalse($f->isInIncrementsOfRanges(3, '*'));

$this->assertTrue($f->isInIncrementsOfRanges(4, '4/10'));
$this->assertTrue($f->isInIncrementsOfRanges(14, '4/10'));
Expand Down

0 comments on commit 99d5db9

Please sign in to comment.