From 85a33a10ef5c2f5257915ccb7dc44acc8e8faeaa Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Mon, 25 Mar 2019 20:30:02 +0100 Subject: [PATCH] Fixed bug #77909: DatePeriod::__construct() with invalid recurrence count value Improve error message on invalid reccurence count Adding test when reccurence is -1 --- ext/date/php_date.c | 4 ++++ ...eriod_wrong_recurrence_on_constructor.phpt | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 ext/date/tests/DatePeriod_wrong_recurrence_on_constructor.phpt diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 86a557098c199..8cdf13e3cc49b 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -4578,6 +4578,10 @@ PHP_METHOD(DatePeriod, __construct) dpobj->end = clone; } } + + if (dpobj->end == NULL && recurrences < 1) { + php_error_docref(NULL, E_WARNING, "The recurrence count '%d' is invalid. Needs to be > 0", (int) recurrences); + } /* options */ dpobj->include_start_date = !(options & PHP_DATE_PERIOD_EXCLUDE_START_DATE); diff --git a/ext/date/tests/DatePeriod_wrong_recurrence_on_constructor.phpt b/ext/date/tests/DatePeriod_wrong_recurrence_on_constructor.phpt new file mode 100644 index 0000000000000..715ea63dc94db --- /dev/null +++ b/ext/date/tests/DatePeriod_wrong_recurrence_on_constructor.phpt @@ -0,0 +1,19 @@ +--TEST-- +DatePeriod: Test wrong recurrence parameter on __construct +--FILE-- +getMessage(), "\n"; +} + +try { + new DatePeriod(new DateTime('yesterday'), new DateInterval('P1D'),-1); +} catch (Exception $exception) { + echo $exception->getMessage(), "\n"; +} +?> +--EXPECTF-- +DatePeriod::__construct(): The recurrence count '0' is invalid. Needs to be > 0 +DatePeriod::__construct(): The recurrence count '-1' is invalid. Needs to be > 0