Skip to content

Commit e8678fc

Browse files
committed
Fixed bug #75902
Don't special-case nested arrays/objects in str_replace(), instead perform a string cast on them as well. For arrays, this will always result in the usual conversion warning. This behavior is consistent with preg_replace(). If we didn't want to cast the array to string here, we should instead perform the replacement recursively. Silently copying it is just confusing.
1 parent d9219f9 commit e8678fc

File tree

5 files changed

+59
-48
lines changed

5 files changed

+59
-48
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ PHP NEWS
119119
filter). (kkopachev)
120120
. Fixed bug #78385 (parse_url() does not include 'query' when question mark
121121
is the last char). (Islam Israfilov)
122+
. Fixed bug #75902 (str_replace should warn when misused with nested arrays).
123+
(Nikita)
122124
. Made quoting of cmd execution functions consistent. (cmb)
123125

124126
- tidy:

ext/standard/string.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,15 +4326,12 @@ static void php_str_replace_common(INTERNAL_FUNCTION_PARAMETERS, int case_sensit
43264326
/* For each subject entry, convert it to string, then perform replacement
43274327
and add the result to the return_value array. */
43284328
ZEND_HASH_FOREACH_KEY_VAL(subject_ht, num_key, string_key, subject_entry) {
4329+
zend_string *tmp_subject_str;
43294330
ZVAL_DEREF(subject_entry);
4330-
if (Z_TYPE_P(subject_entry) != IS_ARRAY && Z_TYPE_P(subject_entry) != IS_OBJECT) {
4331-
zend_string *tmp_subject_str;
4332-
subject_str = zval_get_tmp_string(subject_entry, &tmp_subject_str);
4333-
count += php_str_replace_in_subject(search, replace, subject_str, &result, case_sensitivity);
4334-
zend_tmp_string_release(tmp_subject_str);
4335-
} else {
4336-
ZVAL_COPY(&result, subject_entry);
4337-
}
4331+
subject_str = zval_get_tmp_string(subject_entry, &tmp_subject_str);
4332+
count += php_str_replace_in_subject(search, replace, subject_str, &result, case_sensitivity);
4333+
zend_tmp_string_release(tmp_subject_str);
4334+
43384335
/* Add to return array */
43394336
if (string_key) {
43404337
zend_hash_add_new(Z_ARRVAL_P(return_value), string_key, &result);

ext/standard/tests/strings/bug25671.phpt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@ Bug #25671 (subarrays not copied correctly)
1515
echo serialize(str_replace(" ", "", $arr)) . "\n";
1616
echo serialize(str_replace(" ", "", $arr)) . "\n";
1717
?>
18-
--EXPECT--
19-
a:4:{i:0;s:19:"This is strung one.";i:1;s:19:"This is strung two.";i:2;a:2:{i:0;s:23:"This is another string.";i:1;s:22:"This is a last string.";}i:3;s:22:"This is a last strung.";}
20-
a:4:{i:0;s:19:"This is strung one.";i:1;s:19:"This is strung two.";i:2;a:2:{i:0;s:23:"This is another string.";i:1;s:22:"This is a last string.";}i:3;s:22:"This is a last strung.";}
21-
a:4:{i:0;s:16:"Thisisstringone.";i:1;s:16:"Thisisstringtwo.";i:2;a:2:{i:0;s:23:"This is another string.";i:1;s:22:"This is a last string.";}i:3;s:18:"Thisisalaststring.";}
22-
a:4:{i:0;s:16:"Thisisstringone.";i:1;s:16:"Thisisstringtwo.";i:2;a:2:{i:0;s:23:"This is another string.";i:1;s:22:"This is a last string.";}i:3;s:18:"Thisisalaststring.";}
18+
--EXPECTF--
19+
Warning: Array to string conversion in %s on line %d
20+
a:4:{i:0;s:19:"This is strung one.";i:1;s:19:"This is strung two.";i:2;s:5:"Array";i:3;s:22:"This is a last strung.";}
21+
22+
Warning: Array to string conversion in %s on line %d
23+
a:4:{i:0;s:19:"This is strung one.";i:1;s:19:"This is strung two.";i:2;s:5:"Array";i:3;s:22:"This is a last strung.";}
24+
25+
Warning: Array to string conversion in %s on line %d
26+
a:4:{i:0;s:16:"Thisisstringone.";i:1;s:16:"Thisisstringtwo.";i:2;s:5:"Array";i:3;s:18:"Thisisalaststring.";}
27+
28+
Warning: Array to string conversion in %s on line %d
29+
a:4:{i:0;s:16:"Thisisstringone.";i:1;s:16:"Thisisstringtwo.";i:2;s:5:"Array";i:3;s:18:"Thisisalaststring.";}

ext/standard/tests/strings/bug71969.phpt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,9 @@ foreach($a as &$record)
1313
}
1414
var_dump(str_replace("2", "3", $a));
1515
?>
16-
--EXPECT--
16+
--EXPECTF--
17+
Warning: Array to string conversion in %s on line %d
1718
array(1) {
1819
[0]=>
19-
array(1) {
20-
["one"]=>
21-
array(2) {
22-
["a"]=>
23-
string(4) "2222"
24-
["b"]=>
25-
string(4) "1111"
26-
}
27-
}
20+
string(5) "Array"
2821
}

ext/standard/tests/strings/str_replace_variation1.phpt

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ foreach( $search_arr as $value ) {
2727
}
2828

2929
?>
30-
--EXPECT--
30+
--EXPECTF--
3131
*** Testing str_replace() with various search values ***
3232
-- Iteration 0 --
33+
34+
Warning: Array to string conversion in %s on line %d
3335
array(12) {
3436
[0]=>
3537
string(5) "FOUND"
@@ -50,8 +52,7 @@ array(12) {
5052
[8]=>
5153
string(0) ""
5254
[9]=>
53-
array(0) {
54-
}
55+
string(5) "Array"
5556
[10]=>
5657
string(3) "php"
5758
[11]=>
@@ -60,6 +61,8 @@ array(12) {
6061
int(5)
6162

6263
-- Iteration 1 --
64+
65+
Warning: Array to string conversion in %s on line %d
6366
array(12) {
6467
[0]=>
6568
string(1) "1"
@@ -80,8 +83,7 @@ array(12) {
8083
[8]=>
8184
string(0) ""
8285
[9]=>
83-
array(0) {
84-
}
86+
string(5) "Array"
8587
[10]=>
8688
string(3) "php"
8789
[11]=>
@@ -90,6 +92,8 @@ array(12) {
9092
int(0)
9193

9294
-- Iteration 2 --
95+
96+
Warning: Array to string conversion in %s on line %d
9397
array(12) {
9498
[0]=>
9599
string(5) "FOUND"
@@ -110,8 +114,7 @@ array(12) {
110114
[8]=>
111115
string(0) ""
112116
[9]=>
113-
array(0) {
114-
}
117+
string(5) "Array"
115118
[10]=>
116119
string(3) "php"
117120
[11]=>
@@ -120,6 +123,8 @@ array(12) {
120123
int(5)
121124

122125
-- Iteration 3 --
126+
127+
Warning: Array to string conversion in %s on line %d
123128
array(12) {
124129
[0]=>
125130
string(1) "1"
@@ -140,8 +145,7 @@ array(12) {
140145
[8]=>
141146
string(0) ""
142147
[9]=>
143-
array(0) {
144-
}
148+
string(5) "Array"
145149
[10]=>
146150
string(3) "php"
147151
[11]=>
@@ -150,6 +154,8 @@ array(12) {
150154
int(2)
151155

152156
-- Iteration 4 --
157+
158+
Warning: Array to string conversion in %s on line %d
153159
array(12) {
154160
[0]=>
155161
string(1) "1"
@@ -170,8 +176,7 @@ array(12) {
170176
[8]=>
171177
string(0) ""
172178
[9]=>
173-
array(0) {
174-
}
179+
string(5) "Array"
175180
[10]=>
176181
string(3) "php"
177182
[11]=>
@@ -180,6 +185,8 @@ array(12) {
180185
int(2)
181186

182187
-- Iteration 5 --
188+
189+
Warning: Array to string conversion in %s on line %d
183190
array(12) {
184191
[0]=>
185192
string(5) "FOUND"
@@ -200,8 +207,7 @@ array(12) {
200207
[8]=>
201208
string(0) ""
202209
[9]=>
203-
array(0) {
204-
}
210+
string(5) "Array"
205211
[10]=>
206212
string(3) "php"
207213
[11]=>
@@ -210,6 +216,8 @@ array(12) {
210216
int(5)
211217

212218
-- Iteration 6 --
219+
220+
Warning: Array to string conversion in %s on line %d
213221
array(12) {
214222
[0]=>
215223
string(1) "1"
@@ -230,8 +238,7 @@ array(12) {
230238
[8]=>
231239
string(0) ""
232240
[9]=>
233-
array(0) {
234-
}
241+
string(5) "Array"
235242
[10]=>
236243
string(3) "php"
237244
[11]=>
@@ -240,6 +247,8 @@ array(12) {
240247
int(2)
241248

242249
-- Iteration 7 --
250+
251+
Warning: Array to string conversion in %s on line %d
243252
array(12) {
244253
[0]=>
245254
string(1) "1"
@@ -260,8 +269,7 @@ array(12) {
260269
[8]=>
261270
string(0) ""
262271
[9]=>
263-
array(0) {
264-
}
272+
string(5) "Array"
265273
[10]=>
266274
string(3) "php"
267275
[11]=>
@@ -270,6 +278,8 @@ array(12) {
270278
int(2)
271279

272280
-- Iteration 8 --
281+
282+
Warning: Array to string conversion in %s on line %d
273283
array(12) {
274284
[0]=>
275285
string(1) "1"
@@ -290,8 +300,7 @@ array(12) {
290300
[8]=>
291301
string(0) ""
292302
[9]=>
293-
array(0) {
294-
}
303+
string(5) "Array"
295304
[10]=>
296305
string(3) "php"
297306
[11]=>
@@ -300,6 +309,8 @@ array(12) {
300309
int(0)
301310

302311
-- Iteration 9 --
312+
313+
Warning: Array to string conversion in %s on line %d
303314
array(12) {
304315
[0]=>
305316
string(1) "1"
@@ -320,8 +331,7 @@ array(12) {
320331
[8]=>
321332
string(0) ""
322333
[9]=>
323-
array(0) {
324-
}
334+
string(5) "Array"
325335
[10]=>
326336
string(3) "php"
327337
[11]=>
@@ -330,6 +340,8 @@ array(12) {
330340
int(0)
331341

332342
-- Iteration 10 --
343+
344+
Warning: Array to string conversion in %s on line %d
333345
array(12) {
334346
[0]=>
335347
string(1) "1"
@@ -350,8 +362,7 @@ array(12) {
350362
[8]=>
351363
string(0) ""
352364
[9]=>
353-
array(0) {
354-
}
365+
string(5) "Array"
355366
[10]=>
356367
string(5) "FOUND"
357368
[11]=>
@@ -360,6 +371,8 @@ array(12) {
360371
int(1)
361372

362373
-- Iteration 11 --
374+
375+
Warning: Array to string conversion in %s on line %d
363376
array(12) {
364377
[0]=>
365378
string(1) "1"
@@ -380,8 +393,7 @@ array(12) {
380393
[8]=>
381394
string(0) ""
382395
[9]=>
383-
array(0) {
384-
}
396+
string(5) "Array"
385397
[10]=>
386398
string(3) "php"
387399
[11]=>

0 commit comments

Comments
 (0)