Skip to content

Commit cb9c99e

Browse files
committed
Remove preg_replace /e modifier
1 parent 4d3e4d3 commit cb9c99e

File tree

7 files changed

+20
-152
lines changed

7 files changed

+20
-152
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
. Fixed bug #60509 (pcntl_signal doesn't decrease ref-count of old handler
8686
when setting SIG_DFL). (Julien)
8787

88+
- PCRE:
89+
. Removed support for the /e (PREG_REPLACE_EVAL) modifier. (Nikita)
90+
8891
- PDO_mysql:
8992
. Fixed bug #68424 (Add new PDO mysql connection attr to control multi
9093
statements option). (peter dot wolanin at acquia dot com)

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ PHP X.Y UPGRADE NOTES
7979
. gmp_setbit() and gmp_clrbit() now return FALSE for negative indices, making
8080
them consistent with other GMP functions.
8181

82+
- PCRE:
83+
. Removed support for /e (PREG_REPLACE_EVAL) modifier. Use
84+
preg_reaplace_callback() instead.
85+
8286
- Standard:
8387
. Removed string category support in setlocale(). Use the LC_* constants
8488
instead.

ext/pcre/php_pcre.c

Lines changed: 11 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -986,88 +986,6 @@ static zend_string *preg_do_repl_func(zval *function, char *subject, int *offset
986986
}
987987
/* }}} */
988988

989-
/* {{{ preg_do_eval
990-
*/
991-
static zend_string *preg_do_eval(char *eval_str, int eval_str_len, char *subject,
992-
int *offsets, int count)
993-
{
994-
zval retval; /* Return value from evaluation */
995-
char *eval_str_end, /* End of eval string */
996-
*match, /* Current match for a backref */
997-
*walk, /* Used to walk the code string */
998-
*segment, /* Start of segment to append while walking */
999-
walk_last; /* Last walked character */
1000-
int match_len; /* Length of the match */
1001-
int backref; /* Current backref */
1002-
zend_string *esc_match; /* Quote-escaped match */
1003-
zend_string *result_str;
1004-
char *compiled_string_description;
1005-
smart_str code = {0};
1006-
1007-
eval_str_end = eval_str + eval_str_len;
1008-
walk = segment = eval_str;
1009-
walk_last = 0;
1010-
1011-
while (walk < eval_str_end) {
1012-
/* If found a backreference.. */
1013-
if ('\\' == *walk || '$' == *walk) {
1014-
smart_str_appendl(&code, segment, walk - segment);
1015-
if (walk_last == '\\') {
1016-
code.s->val[code.s->len-1] = *walk++;
1017-
segment = walk;
1018-
walk_last = 0;
1019-
continue;
1020-
}
1021-
segment = walk;
1022-
if (preg_get_backref(&walk, &backref)) {
1023-
if (backref < count) {
1024-
/* Find the corresponding string match and substitute it
1025-
in instead of the backref */
1026-
match = subject + offsets[backref<<1];
1027-
match_len = offsets[(backref<<1)+1] - offsets[backref<<1];
1028-
if (match_len) {
1029-
esc_match = php_addslashes(zend_string_init(match, match_len, 0), 1);
1030-
} else {
1031-
esc_match = zend_string_init(match, match_len, 0);
1032-
}
1033-
} else {
1034-
esc_match = STR_EMPTY_ALLOC();
1035-
}
1036-
smart_str_appendl(&code, esc_match->val, esc_match->len);
1037-
1038-
segment = walk;
1039-
1040-
/* Clean up and reassign */
1041-
zend_string_release(esc_match);
1042-
continue;
1043-
}
1044-
}
1045-
walk++;
1046-
walk_last = walk[-1];
1047-
}
1048-
smart_str_appendl(&code, segment, walk - segment);
1049-
smart_str_0(&code);
1050-
1051-
compiled_string_description = zend_make_compiled_string_description("regexp code");
1052-
/* Run the code */
1053-
if (zend_eval_stringl(code.s->val, code.s->len, &retval, compiled_string_description) == FAILURE) {
1054-
efree(compiled_string_description);
1055-
php_error_docref(NULL,E_ERROR, "Failed evaluating code: %s%s", PHP_EOL, code.s->val);
1056-
/* zend_error() does not return in this case */
1057-
}
1058-
efree(compiled_string_description);
1059-
1060-
/* Save the return string */
1061-
result_str = zval_get_string(&retval);
1062-
1063-
/* Clean up */
1064-
zval_dtor(&retval);
1065-
smart_str_free(&code);
1066-
1067-
return result_str;
1068-
}
1069-
/* }}} */
1070-
1071989
/* {{{ php_pcre_replace
1072990
*/
1073991
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,
11031021
int alloc_len; /* Actual allocated length */
11041022
int match_len; /* Length of the current match */
11051023
int backref; /* Backreference number */
1106-
int eval; /* If the replacement string should be eval'ed */
11071024
int start_offset; /* Where the new search starts */
11081025
int g_notempty=0; /* If the match should not be empty */
11091026
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,
11171034
int result_len; /* Length of result */
11181035
unsigned char *mark = NULL; /* Target for MARK name */
11191036
zend_string *result; /* Result of replacement */
1120-
zend_string *eval_result=NULL; /* Result of eval or custom function */
1037+
zend_string *eval_result=NULL; /* Result of custom function */
11211038
ALLOCA_FLAG(use_heap);
11221039

11231040
if (extra == NULL) {
@@ -1127,22 +1044,16 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, char *subject,
11271044
extra->match_limit = (unsigned long)PCRE_G(backtrack_limit);
11281045
extra->match_limit_recursion = (unsigned long)PCRE_G(recursion_limit);
11291046

1130-
eval = pce->preg_options & PREG_REPLACE_EVAL;
1131-
if (is_callable_replace) {
1132-
if (eval) {
1133-
php_error_docref(NULL, E_WARNING, "Modifier /e cannot be used with replacement callback");
1134-
return NULL;
1135-
}
1136-
} else {
1047+
if (pce->preg_options & PREG_REPLACE_EVAL) {
1048+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The /e modifier is no longer supported, use preg_replace_callback instead");
1049+
return NULL;
1050+
}
1051+
if (!is_callable_replace) {
11371052
replace = Z_STRVAL_P(replace_val);
11381053
replace_len = (int)Z_STRLEN_P(replace_val);
11391054
replace_end = replace + replace_len;
11401055
}
11411056

1142-
if (eval) {
1143-
php_error_docref(NULL, E_DEPRECATED, "The /e modifier is deprecated, use preg_replace_callback instead");
1144-
}
1145-
11461057
/* Calculate the size of the offsets array, and allocate memory for it. */
11471058
num_subpats = pce->capture_count + 1;
11481059
size_offsets = num_subpats * 3;
@@ -1201,13 +1112,8 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, char *subject,
12011112
match = subject + offsets[0];
12021113

12031114
new_len = result_len + offsets[0] - start_offset; /* part before the match */
1204-
1205-
/* If evaluating, do it and add the return string's length */
1206-
if (eval) {
1207-
eval_result = preg_do_eval(replace, replace_len, subject,
1208-
offsets, count);
1209-
new_len += (int)eval_result->len;
1210-
} else if (is_callable_replace) {
1115+
1116+
if (is_callable_replace) {
12111117
/* Use custom function to get replacement string and its length. */
12121118
eval_result = preg_do_repl_func(replace_val, subject, offsets, subpat_names, count, mark);
12131119
new_len += (int)eval_result->len;
@@ -1243,10 +1149,9 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, char *subject,
12431149

12441150
/* copy replacement and backrefs */
12451151
walkbuf = result->val + result_len;
1246-
1247-
/* If evaluating or using custom function, copy result to the buffer
1248-
* and clean up. */
1249-
if (eval || is_callable_replace) {
1152+
1153+
/* If using custom function, copy result to the buffer and clean up. */
1154+
if (is_callable_replace) {
12501155
memcpy(walkbuf, eval_result->val, eval_result->len);
12511156
result_len += (int)eval_result->len;
12521157
if (eval_result) zend_string_release(eval_result);

ext/pcre/tests/002.phpt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,5 @@ string(12) "a${1b${1c${1"
3434
Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 8 in %s002.php on line 11
3535
NULL
3636

37-
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in %s on line 12
38-
39-
Parse error: %s in %s002.php(12) : regexp code on line 1
40-
41-
Fatal error: preg_replace(): Failed evaluating code:
42-
for ($ in %s002.php on line 12
37+
Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in %s on line 12
38+
NULL

ext/pcre/tests/004.phpt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ var_dump($m);
1212
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));
1313
var_dump($m);
1414

15-
var_dump(preg_replace(array('@//.*@S', '@/\*.*\*/@SsUe'), array('', 'preg_replace("/[^\r\n]+/S", "", \'$0\')'), "hello\n//x \n/*\ns\n*/"));
16-
1715
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));
1816
?>
1917
--EXPECTF--
@@ -117,13 +115,6 @@ array(1) {
117115
}
118116
}
119117
}
120-
121-
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in %s on line %d
122-
string(9) "hello
123-
124-
125-
126-
"
127118
array(3) {
128119
[0]=>
129120
array(2) {

ext/pcre/tests/preg_replace.phpt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,8 @@ var_dump(preg_replace('{{\D+}}', 'ddd', 'abcd'));
88

99
var_dump(preg_replace('/(ab)(c)(d)(e)(f)(g)(h)(i)(j)(k)/', 'a${1}2$103', 'zabcdefghijkl'));
1010

11-
var_dump(preg_replace_callback('//e', '', ''));
12-
13-
var_dump(preg_replace_callback('//e', 'strtolower', ''));
14-
1511
?>
1612
--EXPECTF--
1713
string(1) "x"
1814
string(4) "abcd"
1915
string(8) "zaab2k3l"
20-
21-
Warning: preg_replace_callback(): Requires argument 2, '', to be a valid callback in %spreg_replace.php on line 8
22-
string(0) ""
23-
24-
Warning: preg_replace_callback(): Modifier /e cannot be used with replacement callback in %spreg_replace.php on line 10
25-
NULL

tests/lang/bug24403.phpt

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)