Skip to content

Commit 8317de6

Browse files
committed
Fix GH-14709 overflow on reccurences for DatePeriod::__construct
1 parent 42908f9 commit 8317de6

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

ext/date/php_date.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4798,9 +4798,9 @@ PHP_METHOD(DatePeriod, __construct)
47984798
}
47994799
}
48004800

4801-
if (dpobj->end == NULL && recurrences < 1) {
4801+
if (dpobj->end == NULL && (recurrences < 1 || ZEND_LONG_INT_OVFL(recurrences))) {
48024802
zend_string *func = get_active_function_or_method_name();
4803-
zend_throw_exception_ex(NULL, 0, "%s(): Recurrence count must be greater than 0", ZSTR_VAL(func));
4803+
zend_throw_exception_ex(NULL, 0, "%s(): Recurrence count must be between 1 and %d", ZSTR_VAL(func), INT_MAX);
48044804
zend_string_release(func);
48054805
RETURN_THROWS();
48064806
}
@@ -4809,7 +4809,7 @@ PHP_METHOD(DatePeriod, __construct)
48094809
dpobj->include_start_date = !(options & PHP_DATE_PERIOD_EXCLUDE_START_DATE);
48104810
dpobj->include_end_date = options & PHP_DATE_PERIOD_INCLUDE_END_DATE;
48114811

4812-
/* recurrrences */
4812+
/* recurrences */
48134813
dpobj->recurrences = recurrences + dpobj->include_start_date + dpobj->include_end_date;
48144814

48154815
dpobj->initialized = 1;

ext/date/tests/DatePeriod_wrong_recurrence_on_constructor.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ try {
1515
}
1616

1717
?>
18-
--EXPECT--
19-
DatePeriod::__construct(): Recurrence count must be greater than 0
20-
DatePeriod::__construct(): Recurrence count must be greater than 0
18+
--EXPECTF--
19+
DatePeriod::__construct(): Recurrence count must be between 1 and %d
20+
DatePeriod::__construct(): Recurrence count must be between 1 and %d

ext/date/tests/gh14709.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Bug GH-14709 overflow on reccurences parameter
3+
--SKIPIF--
4+
<?php
5+
if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only");
6+
?>
7+
--FILE--
8+
<?php
9+
$start = new DateTime('2018-12-31 00:00:00');
10+
$interval = new DateInterval('P1M');
11+
12+
try {
13+
new DatePeriod($start, $interval, PHP_INT_MAX);
14+
} catch (Exception $e) {
15+
echo $e->getMessage();
16+
}
17+
?>
18+
--EXPECTF--
19+
DatePeriod::__construct(): Recurrence count must be between 1 and %d

0 commit comments

Comments
 (0)