Skip to content

Commit ad4cfa9

Browse files
committed
changes from feedback
1 parent 286533e commit ad4cfa9

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

ext/intl/dateformat/dateformat.stub.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ public function parse(string $string, &$offset = null): int|float|false {}
161161

162162
/**
163163
* @param int $offset
164-
* @tentative-return-type
165164
*/
166165
public function parseToCalendar(string $string, &$offset = null): int|float|false {}
167166

ext/intl/dateformat/dateformat_arginfo.h

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/intl/dateformat/dateformat_parse.c

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* tex
4343
INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
4444

4545
if (UNEXPECTED(update_calendar)) {
46-
UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
47-
udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
48-
if( text_utf16 ){
46+
UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
47+
udat_parseCalendar(DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
48+
if (text_utf16) {
4949
efree(text_utf16);
5050
}
5151
INTL_METHOD_CHECK_STATUS( dfo, "Calendar parsing failed" );
5252
timestamp = ucal_getMillis( parsed_calendar, &INTL_DATA_ERROR_CODE(dfo));
5353
} else {
54-
timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
55-
if( text_utf16 ){
54+
timestamp = udat_parse(DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
55+
if (text_utf16) {
5656
efree(text_utf16);
5757
}
5858
}
@@ -154,12 +154,12 @@ PHP_FUNCTION(datefmt_parse)
154154
RETURN_FALSE;
155155
}
156156
parse_pos = (int32_t)long_parse_pos;
157-
if((size_t)parse_pos > text_len) {
157+
if ((size_t)parse_pos > text_len) {
158158
RETURN_FALSE;
159159
}
160160
}
161-
internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, false, return_value);
162-
if(z_parse_pos) {
161+
internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos ? &parse_pos : NULL, false, return_value);
162+
if (z_parse_pos) {
163163
zval_ptr_dtor(z_parse_pos);
164164
ZVAL_LONG(z_parse_pos, parse_pos);
165165
}
@@ -168,38 +168,44 @@ PHP_FUNCTION(datefmt_parse)
168168

169169
PHP_METHOD(IntlDateFormatter, parseToCalendar)
170170
{
171-
char* text_to_parse = NULL;
172-
size_t text_len =0;
173-
zval* z_parse_pos = NULL;
174-
int32_t parse_pos = -1;
171+
zend_string *text_to_parse = NULL;
172+
zval* z_parse_pos = NULL;
173+
int32_t parse_pos = -1;
175174

176175
DATE_FORMAT_METHOD_INIT_VARS;
177176

178-
/* Parse parameters. */
179-
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|z!",
180-
&object, IntlDateFormatter_ce_ptr, &text_to_parse, &text_len, &z_parse_pos ) == FAILURE ){
181-
RETURN_THROWS();
182-
}
177+
ZEND_PARSE_PARAMETERS_START(1, 2)
178+
Z_PARAM_STR(text_to_parse)
179+
Z_PARAM_OPTIONAL
180+
Z_PARAM_ZVAL(z_parse_pos)
181+
ZEND_PARSE_PARAMETERS_END();
182+
183+
object = ZEND_THIS;
183184

184185
/* Fetch the object. */
185186
DATE_FORMAT_METHOD_FETCH_OBJECT;
186187

187188
if (z_parse_pos) {
188189
zend_long long_parse_pos;
189190
ZVAL_DEREF(z_parse_pos);
190-
long_parse_pos = zval_get_long(z_parse_pos);
191+
bool failed = false;
192+
long_parse_pos = zval_try_get_long(z_parse_pos, &failed);
193+
if (failed) {
194+
zend_argument_type_error(2, "must be of type int, %s given", zend_zval_value_name(z_parse_pos));
195+
RETURN_THROWS();
196+
}
191197
if (ZEND_LONG_INT_OVFL(long_parse_pos)) {
192198
intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
193199
intl_error_set_custom_msg(NULL, "String index is out of valid range.", 0);
194200
RETURN_FALSE;
195201
}
196202
parse_pos = (int32_t)long_parse_pos;
197-
if((size_t)parse_pos > text_len) {
203+
if (parse_pos != -1 && (size_t)parse_pos > ZSTR_LEN(text_to_parse)) {
198204
RETURN_FALSE;
199205
}
200206
}
201-
internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, true, return_value);
202-
if(z_parse_pos) {
207+
internal_parse_to_timestamp( dfo, ZSTR_VAL(text_to_parse), ZSTR_LEN(text_to_parse), z_parse_pos ? &parse_pos : NULL, true, return_value);
208+
if (z_parse_pos) {
203209
zval_ptr_dtor(z_parse_pos);
204210
ZVAL_LONG(z_parse_pos, parse_pos);
205211
}

ext/intl/tests/gh13766.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,24 @@ var_dump($oIntlDateFormatter->parse('America/Los_Angeles', $offset1));
1212
var_dump($oIntlDateFormatter->getTimeZone()->getID());
1313
var_dump($oIntlDateFormatter->parseToCalendar('America/Los_Angeles', $offset2));
1414
var_dump($oIntlDateFormatter->getTimeZone()->getID());
15+
$offset3 = "offset";
16+
17+
try {
18+
$oIntlDateFormatter->parseToCalendar('America/Los_Angeles', $offset3);
19+
} catch (\TypeError $e) {
20+
echo $e->getMessage() . PHP_EOL;
21+
}
22+
$offset3 = PHP_INT_MAX * 16;
23+
try {
24+
$oIntlDateFormatter->parseToCalendar('America/Los_Angeles', $offset3);
25+
} catch (\ValueError $e) {
26+
echo $e->getMessage();
27+
}
1528
--EXPECTF--
1629
int(%d)
1730
string(13) "Europe/Berlin"
1831
int(%d)
1932
string(19) "America/Los_Angeles"
33+
IntlDateFormatter::parseToCalendar(): Argument #2 ($offset) must be of type int, string given
34+
35+
Deprecated: Implicit conversion from float 1.4757395258967641E+20 to int loses precision in %s on line %d

0 commit comments

Comments
 (0)