Skip to content

Commit f1e1231

Browse files
committed
Fix strtr() segfault
1 parent 1616415 commit f1e1231

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

ext/standard/string.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,17 +2855,11 @@ static void php_strtr_array(zval *return_value, char *str, int slen, HashTable *
28552855
if (len > slen - pos) continue;
28562856
entry = zend_hash_str_find(pats, key, len);
28572857
if (entry != NULL) {
2858-
if (UNEXPECTED(Z_TYPE_P(entry) != IS_STRING)) {
2859-
ZVAL_DUP(&tmp, entry);
2860-
convert_to_string(&tmp);
2861-
entry = &tmp;
2862-
}
2863-
smart_str_appendl(&result, Z_STRVAL_P(entry), Z_STRLEN_P(entry));
2858+
zend_string *str = zval_get_string(entry);
2859+
smart_str_appendl(&result, str->val, str->len);
28642860
pos += len;
2865-
if (entry == &tmp) {
2866-
zval_dtor(&tmp);
2867-
}
28682861
found = 1;
2862+
STR_RELEASE(str);
28692863
break;
28702864
}
28712865
} ZEND_HASH_FOREACH_END();
@@ -2886,17 +2880,11 @@ static void php_strtr_array(zval *return_value, char *str, int slen, HashTable *
28862880
for (len = maxlen; len >= minlen; len--) {
28872881
entry = zend_hash_str_find(pats, key, len);
28882882
if (entry != NULL) {
2889-
if (UNEXPECTED(Z_TYPE_P(entry) != IS_STRING)) {
2890-
ZVAL_DUP(&tmp, entry);
2891-
convert_to_string(&tmp);
2892-
entry = &tmp;
2893-
}
2894-
smart_str_appendl(&result, Z_STRVAL_P(entry), Z_STRLEN_P(entry));
2883+
zend_string *str = zval_get_string(entry);
2884+
smart_str_appendl(&result, str->val, str->len);
28952885
pos += len;
2896-
if (entry == &tmp) {
2897-
zval_dtor(&tmp);
2898-
}
28992886
found = 1;
2887+
STR_RELEASE(str);
29002888
break;
29012889
}
29022890
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
strtr() with references
3+
--FILE--
4+
<?php
5+
6+
$foo = 'foo';
7+
$arr = ['bar' => &$foo];
8+
var_dump(strtr('foobar', $arr));
9+
10+
?>
11+
--EXPECT--
12+
string(6) "foofoo"

0 commit comments

Comments
 (0)