Browse Source

Remove preg_replace /e modifier

pull/1026/head
Nikita Popov 12 years ago
parent
commit
cb9c99ebd0
  1. 3
      NEWS
  2. 4
      UPGRADING
  3. 117
      ext/pcre/php_pcre.c
  4. 8
      ext/pcre/tests/002.phpt
  5. 9
      ext/pcre/tests/004.phpt
  6. 10
      ext/pcre/tests/preg_replace.phpt
  7. 21
      tests/lang/bug24403.phpt

3
NEWS

@ -85,6 +85,9 @@
. Fixed bug #60509 (pcntl_signal doesn't decrease ref-count of old handler
when setting SIG_DFL). (Julien)
- PCRE:
. Removed support for the /e (PREG_REPLACE_EVAL) modifier. (Nikita)
- PDO_mysql:
. Fixed bug #68424 (Add new PDO mysql connection attr to control multi
statements option). (peter dot wolanin at acquia dot com)

4
UPGRADING

@ -79,6 +79,10 @@ PHP X.Y UPGRADE NOTES
. gmp_setbit() and gmp_clrbit() now return FALSE for negative indices, making
them consistent with other GMP functions.
- PCRE:
. Removed support for /e (PREG_REPLACE_EVAL) modifier. Use
preg_reaplace_callback() instead.
- Standard:
. Removed string category support in setlocale(). Use the LC_* constants
instead.

117
ext/pcre/php_pcre.c

@ -986,88 +986,6 @@ static zend_string *preg_do_repl_func(zval *function, char *subject, int *offset
}
/* }}} */
/* {{{ preg_do_eval
*/
static zend_string *preg_do_eval(char *eval_str, int eval_str_len, char *subject,
int *offsets, int count)
{
zval retval; /* Return value from evaluation */
char *eval_str_end, /* End of eval string */
*match, /* Current match for a backref */
*walk, /* Used to walk the code string */
*segment, /* Start of segment to append while walking */
walk_last; /* Last walked character */
int match_len; /* Length of the match */
int backref; /* Current backref */
zend_string *esc_match; /* Quote-escaped match */
zend_string *result_str;
char *compiled_string_description;
smart_str code = {0};
eval_str_end = eval_str + eval_str_len;
walk = segment = eval_str;
walk_last = 0;
while (walk < eval_str_end) {
/* If found a backreference.. */
if ('\\' == *walk || '$' == *walk) {
smart_str_appendl(&code, segment, walk - segment);
if (walk_last == '\\') {
code.s->val[code.s->len-1] = *walk++;
segment = walk;
walk_last = 0;
continue;
}
segment = walk;
if (preg_get_backref(&walk, &backref)) {
if (backref < count) {
/* Find the corresponding string match and substitute it
in instead of the backref */
match = subject + offsets[backref<<1];
match_len = offsets[(backref<<1)+1] - offsets[backref<<1];
if (match_len) {
esc_match = php_addslashes(zend_string_init(match, match_len, 0), 1);
} else {
esc_match = zend_string_init(match, match_len, 0);
}
} else {
esc_match = STR_EMPTY_ALLOC();
}
smart_str_appendl(&code, esc_match->val, esc_match->len);
segment = walk;
/* Clean up and reassign */
zend_string_release(esc_match);
continue;
}
}
walk++;
walk_last = walk[-1];
}
smart_str_appendl(&code, segment, walk - segment);
smart_str_0(&code);
compiled_string_description = zend_make_compiled_string_description("regexp code");
/* Run the code */
if (zend_eval_stringl(code.s->val, code.s->len, &retval, compiled_string_description) == FAILURE) {
efree(compiled_string_description);
php_error_docref(NULL,E_ERROR, "Failed evaluating code: %s%s", PHP_EOL, code.s->val);
/* zend_error() does not return in this case */
}
efree(compiled_string_description);
/* Save the return string */
result_str = zval_get_string(&retval);
/* Clean up */
zval_dtor(&retval);
smart_str_free(&code);
return result_str;
}
/* }}} */
/* {{{ php_pcre_replace
*/
PHPAPI zend_string *php_pcre_replace(zend_string *regex,
@ -1103,7 +1021,6 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, char *subject,
int alloc_len; /* Actual allocated length */
int match_len; /* Length of the current match */
int backref; /* Backreference number */
int eval; /* If the replacement string should be eval'ed */
int start_offset; /* Where the new search starts */
int g_notempty=0; /* If the match should not be empty */
int replace_len=0; /* Length of replacement string */
@ -1117,7 +1034,7 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, char *subject,
int result_len; /* Length of result */
unsigned char *mark = NULL; /* Target for MARK name */
zend_string *result; /* Result of replacement */
zend_string *eval_result=NULL; /* Result of eval or custom function */
zend_string *eval_result=NULL; /* Result of custom function */
ALLOCA_FLAG(use_heap);
if (extra == NULL) {
@ -1127,22 +1044,16 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, char *subject,
extra->match_limit = (unsigned long)PCRE_G(backtrack_limit);
extra->match_limit_recursion = (unsigned long)PCRE_G(recursion_limit);
eval = pce->preg_options & PREG_REPLACE_EVAL;
if (is_callable_replace) {
if (eval) {
php_error_docref(NULL, E_WARNING, "Modifier /e cannot be used with replacement callback");
return NULL;
}
} else {
if (pce->preg_options & PREG_REPLACE_EVAL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The /e modifier is no longer supported, use preg_replace_callback instead");
return NULL;
}
if (!is_callable_replace) {
replace = Z_STRVAL_P(replace_val);
replace_len = (int)Z_STRLEN_P(replace_val);
replace_end = replace + replace_len;
}
if (eval) {
php_error_docref(NULL, E_DEPRECATED, "The /e modifier is deprecated, use preg_replace_callback instead");
}
/* Calculate the size of the offsets array, and allocate memory for it. */
num_subpats = pce->capture_count + 1;
size_offsets = num_subpats * 3;
@ -1201,13 +1112,8 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, char *subject,
match = subject + offsets[0];
new_len = result_len + offsets[0] - start_offset; /* part before the match */
/* If evaluating, do it and add the return string's length */
if (eval) {
eval_result = preg_do_eval(replace, replace_len, subject,
offsets, count);
new_len += (int)eval_result->len;
} else if (is_callable_replace) {
if (is_callable_replace) {
/* Use custom function to get replacement string and its length. */
eval_result = preg_do_repl_func(replace_val, subject, offsets, subpat_names, count, mark);
new_len += (int)eval_result->len;
@ -1243,10 +1149,9 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, char *subject,
/* copy replacement and backrefs */
walkbuf = result->val + result_len;
/* If evaluating or using custom function, copy result to the buffer
* and clean up. */
if (eval || is_callable_replace) {
/* If using custom function, copy result to the buffer and clean up. */
if (is_callable_replace) {
memcpy(walkbuf, eval_result->val, eval_result->len);
result_len += (int)eval_result->len;
if (eval_result) zend_string_release(eval_result);

8
ext/pcre/tests/002.phpt

@ -34,9 +34,5 @@ string(12) "a${1b${1c${1"
Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 8 in %s002.php on line 11
NULL
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in %s on line 12
Parse error: %s in %s002.php(12) : regexp code on line 1
Fatal error: preg_replace(): Failed evaluating code:
for ($ in %s002.php on line 12
Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in %s on line 12
NULL

9
ext/pcre/tests/004.phpt

@ -12,8 +12,6 @@ var_dump($m);
var_dump(preg_match_all('/zend_parse_parameters(?:_ex\s*\([^,]+,[^,]+|\s*\([^,]+),\s*"([^"]*)"\s*,\s*([^{;]*)/S', 'zend_parse_parameters( 0, "addd|s/", a, b, &c);', $m, PREG_SET_ORDER | PREG_OFFSET_CAPTURE));
var_dump($m);
var_dump(preg_replace(array('@//.*@S', '@/\*.*\*/@SsUe'), array('', 'preg_replace("/[^\r\n]+/S", "", \'$0\')'), "hello\n//x \n/*\ns\n*/"));
var_dump(preg_split('/PHP_(?:NAMED_)?(?:FUNCTION|METHOD)\s*\((\w+(?:,\s*\w+)?)\)/S', "PHP_FUNCTION(s, preg_match)\n{\nlalala", -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE));
?>
--EXPECTF--
@ -117,13 +115,6 @@ array(1) {
}
}
}
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in %s on line %d
string(9) "hello
"
array(3) {
[0]=>
array(2) {

10
ext/pcre/tests/preg_replace.phpt

@ -8,18 +8,8 @@ var_dump(preg_replace('{{\D+}}', 'ddd', 'abcd'));
var_dump(preg_replace('/(ab)(c)(d)(e)(f)(g)(h)(i)(j)(k)/', 'a${1}2$103', 'zabcdefghijkl'));
var_dump(preg_replace_callback('//e', '', ''));
var_dump(preg_replace_callback('//e', 'strtolower', ''));
?>
--EXPECTF--
string(1) "x"
string(4) "abcd"
string(8) "zaab2k3l"
Warning: preg_replace_callback(): Requires argument 2, '', to be a valid callback in %spreg_replace.php on line 8
string(0) ""
Warning: preg_replace_callback(): Modifier /e cannot be used with replacement callback in %spreg_replace.php on line 10
NULL

21
tests/lang/bug24403.phpt

@ -1,21 +0,0 @@
--TEST--
Bug #24403 (scope doesn't properly propagate into internal functions)
--FILE--
<?php
class a
{
public $a = array();
function a()
{
$output = preg_replace(
'!\{\s*([a-z0-9_]+)\s*\}!sie',
"(in_array('\\1',\$this->a) ? '\'.\$p[\'\\1\'].\'' :
'\'.\$r[\'\\1\'].\'')",
"{a} b {c}");
}
}
new a();
?>
--EXPECTF--
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in %s on line %d
Loading…
Cancel
Save