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.
This commit is contained in:
Nikita Popov 2019-10-07 13:04:06 +02:00
parent becda2e041
commit 93ba3abe63
4 changed files with 20 additions and 5 deletions

View File

@ -499,9 +499,8 @@ function ucwords(string $str, string $delimiters = " \t\r\n\f\v"): string {}
/**
* @param string|array $from
* @return string|false
*/
function strtr(string $str, $from, string $to = UNKNOWN) {}
function strtr(string $str, $from, string $to = UNKNOWN): string {}
function strrev(string $str): string {}

View File

@ -659,7 +659,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ucwords, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, delimiters, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_strtr, 0, 0, 2)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_strtr, 0, 2, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_ARG_INFO(0, from)
ZEND_ARG_TYPE_INFO(0, to, IS_STRING, 0)

View File

@ -2819,8 +2819,8 @@ static void php_strtr_array(zval *return_value, zend_string *input, HashTable *p
} else {
len = ZSTR_LEN(str_key);
if (UNEXPECTED(len < 1)) {
efree(num_bitset);
RETURN_FALSE;
php_error_docref(NULL, E_WARNING, "Ignoring replacement of empty string");
continue;
} else if (UNEXPECTED(len > slen)) {
/* skip long patterns */
continue;
@ -3294,6 +3294,7 @@ PHP_FUNCTION(strtr)
}
replace = zval_get_tmp_string(entry, &tmp_replace);
if (ZSTR_LEN(str_key) < 1) {
php_error_docref(NULL, E_WARNING, "Ignoring replacement of empty string");
RETVAL_STR_COPY(str);
} else if (ZSTR_LEN(str_key) == 1) {
RETVAL_STR(php_char_to_str_ex(str,

View File

@ -0,0 +1,15 @@
--TEST--
strtr() trying to replace an empty string
--FILE--
<?php
var_dump(strtr("foo", ["" => "bar"]));
var_dump(strtr("foo", ["" => "bar", "x" => "y"]));
?>
--EXPECTF--
Warning: strtr(): Ignoring replacement of empty string in %s on line %d
string(3) "foo"
Warning: strtr(): Ignoring replacement of empty string in %s on line %d
string(3) "foo"