Skip to content

Commit d69a34a

Browse files
committed
changes from feedback
1 parent f5d9e00 commit d69a34a

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

ext/intl/dateformat/dateformat_parse.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* tex
4545
if (UNEXPECTED(update_calendar)) {
4646
UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
4747
udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
48-
if( text_utf16 ){
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 {
5454
timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
55-
if( text_utf16 ){
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,40 @@ PHP_FUNCTION(datefmt_parse)
168168

169169
PHP_METHOD(IntlDateFormatter, parseToCalendar)
170170
{
171-
char* text_to_parse = NULL;
172-
size_t text_len =0;
171+
zend_string *text_to_parse = NULL;
173172
zval* z_parse_pos = NULL;
174173
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-
if (ZEND_LONG_INT_OVFL(long_parse_pos)) {
191+
bool failed = false;
192+
long_parse_pos = zval_try_get_long(z_parse_pos, &failed);
193+
if (failed || ZEND_LONG_INT_OVFL(long_parse_pos)) {
192194
intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
193-
intl_error_set_custom_msg(NULL, "String index is out of valid range.", 0);
195+
intl_error_set_custom_msg(NULL, "String index is invalid or out of valid range.", 0);
194196
RETURN_FALSE;
195197
}
196198
parse_pos = (int32_t)long_parse_pos;
197-
if((size_t)parse_pos > text_len) {
199+
if(parse_pos != -1 && (size_t)parse_pos > ZSTR_LEN(text_to_parse)) {
198200
RETURN_FALSE;
199201
}
200202
}
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) {
203+
internal_parse_to_timestamp( dfo, ZSTR_VAL(text_to_parse), ZSTR_LEN(text_to_parse), z_parse_pos ? &parse_pos : NULL, true, return_value);
204+
if (z_parse_pos) {
203205
zval_ptr_dtor(z_parse_pos);
204206
ZVAL_LONG(z_parse_pos, parse_pos);
205207
}

0 commit comments

Comments
 (0)