Skip to content

Commit c93b3d1

Browse files
committed
ext/intl: Reject non-finite timestamps in IntlCalendar::setTime() (#25668)
Reject NaN and positive and negative infinity before calling ICU. Report U_ILLEGAL_ARGUMENT_ERROR and preserve the calendar's previous timestamp. Closes #25668
1 parent 0e3c223 commit c93b3d1

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ PHP NEWS
8282
list is empty. (Weilin Du)
8383
. Fixed intl_error_name() truncating error codes outside the int range.
8484
(lacatoire)
85+
. Fixed IntlCalendar::setTime() accepting non-finite timestamps. (Weilin Du)
8586

8687
- JSON:
8788
. Fixed json_decode() and json_validate() error messages. (lacatoire)

‎ext/intl/calendar/calendar_methods.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ U_CFUNC PHP_FUNCTION(intlcal_set_time)
270270

271271
CALENDAR_METHOD_FETCH_OBJECT;
272272

273+
if (UNEXPECTED(!zend_finite(time_arg))) {
274+
intl_errors_set(CALENDAR_ERROR_P(co), U_ILLEGAL_ARGUMENT_ERROR,
275+
"Timestamp must be finite", 0);
276+
RETURN_FALSE;
277+
}
278+
273279
co->ucal->setTime((UDate)time_arg, CALENDAR_ERROR_CODE(co));
274280
INTL_METHOD_CHECK_STATUS(co, "Call to underlying method failed");
275281

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
IntlCalendar::setTime() rejects non-finite timestamps
3+
--CREDITS--
4+
Wong Wai Kin
5+
--EXTENSIONS--
6+
intl
7+
--FILE--
8+
<?php
9+
$calendar = IntlCalendar::createInstance('UTC');
10+
var_dump($calendar->setTime(1234));
11+
12+
foreach ([NAN, INF, -INF] as $timestamp) {
13+
var_dump($calendar->setTime($timestamp));
14+
var_dump($calendar->getErrorCode() === U_ILLEGAL_ARGUMENT_ERROR);
15+
var_dump($calendar->getTime());
16+
}
17+
?>
18+
--EXPECT--
19+
bool(true)
20+
bool(false)
21+
bool(true)
22+
float(1234)
23+
bool(false)
24+
bool(true)
25+
float(1234)
26+
bool(false)
27+
bool(true)
28+
float(1234)

0 commit comments

Comments
 (0)