Skip to content

Commit 69283ba

Browse files
committed
ext/pcre: Refactor php_pcre_replace_func_impl() to not rely on an FCI
1 parent 025cc6f commit 69283ba

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

ext/pcre/php_pcre.c

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,40 +1544,26 @@ static int preg_get_backref(char **str, int *backref)
15441544
}
15451545
/* }}} */
15461546

1547-
/* {{{ preg_do_repl_func */
1548-
static zend_string *preg_do_repl_func(zend_fcall_info *fci, zend_fcall_info_cache *fcc, const char *subject, PCRE2_SIZE *offsets, zend_string **subpat_names, uint32_t num_subpats, int count, const PCRE2_SPTR mark, zend_long flags)
1547+
/* Return NULL if an exception has occurred */
1548+
static zend_string *preg_do_repl_func(zend_fcall_info_cache *fcc, const char *subject, PCRE2_SIZE *offsets, zend_string **subpat_names, uint32_t num_subpats, int count, const PCRE2_SPTR mark, zend_long flags)
15491549
{
1550-
zend_string *result_str;
1550+
zend_string *result_str = NULL;
15511551
zval retval; /* Function return value */
15521552
zval arg; /* Argument to pass to function */
15531553

15541554
array_init_size(&arg, count + (mark ? 1 : 0));
15551555
populate_subpat_array(&arg, subject, offsets, subpat_names, num_subpats, count, mark, flags);
15561556

1557-
fci->retval = &retval;
1558-
fci->param_count = 1;
1559-
fci->params = &arg;
1560-
1561-
if (zend_call_function(fci, fcc) == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
1562-
if (EXPECTED(Z_TYPE(retval) == IS_STRING)) {
1563-
result_str = Z_STR(retval);
1564-
} else {
1565-
result_str = zval_get_string_func(&retval);
1566-
zval_ptr_dtor(&retval);
1567-
}
1568-
} else {
1569-
if (!EG(exception)) {
1570-
php_error_docref(NULL, E_WARNING, "Unable to call custom replacement function");
1571-
}
1572-
1573-
result_str = zend_string_init(&subject[offsets[0]], offsets[1] - offsets[0], 0);
1574-
}
1575-
1557+
zend_call_known_fcc(fcc, &retval, 1, &arg, NULL);
15761558
zval_ptr_dtor(&arg);
1559+
/* No Exception has occurred */
1560+
if (EXPECTED(Z_TYPE(retval) != IS_UNDEF)) {
1561+
result_str = zval_try_get_string(&retval);
1562+
}
1563+
zval_ptr_dtor(&retval);
15771564

15781565
return result_str;
15791566
}
1580-
/* }}} */
15811567

15821568
/* {{{ php_pcre_replace */
15831569
PHPAPI zend_string *php_pcre_replace(zend_string *regex,
@@ -1940,10 +1926,12 @@ static zend_string *php_pcre_replace_func_impl(pcre_cache_entry *pce, zend_strin
19401926

19411927
/* Use custom function to get replacement string and its length. */
19421928
eval_result = preg_do_repl_func(
1943-
fci, fcc, subject, offsets, subpat_names, num_subpats, count,
1929+
fcc, subject, offsets, subpat_names, num_subpats, count,
19441930
pcre2_get_mark(match_data), flags);
19451931

1946-
ZEND_ASSERT(eval_result);
1932+
if (UNEXPECTED(eval_result == NULL)) {
1933+
goto error;
1934+
}
19471935
new_len = zend_safe_address_guarded(1, ZSTR_LEN(eval_result) + ZSTR_MAX_OVERHEAD, new_len) -ZSTR_MAX_OVERHEAD;
19481936
if (new_len >= alloc_len) {
19491937
alloc_len = zend_safe_address_guarded(2, new_len, ZSTR_MAX_OVERHEAD) - ZSTR_MAX_OVERHEAD;

0 commit comments

Comments
 (0)