Skip to content

Commit 93ba3ab

Browse files
committed
Warn on strtr(["" => "x"])
Previously: * If only ["" => "x"] was present, the original string was returned without warning. * If both ["" => "x"] and at least one more element was present, false was returned without warning. New behavior: * Ignore "" keys in the replacement array (and perform any remaining replacement). * Throw a warning indicating that an empty string replacement has been ignored. Closes GH-4792.
1 parent becda2e commit 93ba3ab

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,8 @@ function ucwords(string $str, string $delimiters = " \t\r\n\f\v"): string {}
499499

500500
/**
501501
* @param string|array $from
502-
* @return string|false
503502
*/
504-
function strtr(string $str, $from, string $to = UNKNOWN) {}
503+
function strtr(string $str, $from, string $to = UNKNOWN): string {}
505504

506505
function strrev(string $str): string {}
507506

ext/standard/basic_functions_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ucwords, 0, 1, IS_STRING, 0)
659659
ZEND_ARG_TYPE_INFO(0, delimiters, IS_STRING, 0)
660660
ZEND_END_ARG_INFO()
661661

662-
ZEND_BEGIN_ARG_INFO_EX(arginfo_strtr, 0, 0, 2)
662+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_strtr, 0, 2, IS_STRING, 0)
663663
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
664664
ZEND_ARG_INFO(0, from)
665665
ZEND_ARG_TYPE_INFO(0, to, IS_STRING, 0)

ext/standard/string.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,8 +2819,8 @@ static void php_strtr_array(zval *return_value, zend_string *input, HashTable *p
28192819
} else {
28202820
len = ZSTR_LEN(str_key);
28212821
if (UNEXPECTED(len < 1)) {
2822-
efree(num_bitset);
2823-
RETURN_FALSE;
2822+
php_error_docref(NULL, E_WARNING, "Ignoring replacement of empty string");
2823+
continue;
28242824
} else if (UNEXPECTED(len > slen)) {
28252825
/* skip long patterns */
28262826
continue;
@@ -3294,6 +3294,7 @@ PHP_FUNCTION(strtr)
32943294
}
32953295
replace = zval_get_tmp_string(entry, &tmp_replace);
32963296
if (ZSTR_LEN(str_key) < 1) {
3297+
php_error_docref(NULL, E_WARNING, "Ignoring replacement of empty string");
32973298
RETVAL_STR_COPY(str);
32983299
} else if (ZSTR_LEN(str_key) == 1) {
32993300
RETVAL_STR(php_char_to_str_ex(str,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
strtr() trying to replace an empty string
3+
--FILE--
4+
<?php
5+
6+
var_dump(strtr("foo", ["" => "bar"]));
7+
var_dump(strtr("foo", ["" => "bar", "x" => "y"]));
8+
9+
?>
10+
--EXPECTF--
11+
Warning: strtr(): Ignoring replacement of empty string in %s on line %d
12+
string(3) "foo"
13+
14+
Warning: strtr(): Ignoring replacement of empty string in %s on line %d
15+
string(3) "foo"

0 commit comments

Comments
 (0)