Skip to content

Imply UTF8 validity in implode function #10780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,6 @@ PHP_FUNCTION(explode)
}
/* }}} */

/* {{{ An alias for implode */
/* }}} */

/* {{{ php_implode */
PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return_value)
{
Expand Down Expand Up @@ -958,11 +955,14 @@ PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return

ptr = strings = do_alloca((sizeof(*strings)) * numelems, use_heap);

uint32_t flags = ZSTR_GET_COPYABLE_CONCAT_PROPERTIES(glue);

ZEND_HASH_FOREACH_VAL(pieces, tmp) {
if (EXPECTED(Z_TYPE_P(tmp) == IS_STRING)) {
ptr->str = Z_STR_P(tmp);
len += ZSTR_LEN(ptr->str);
ptr->lval = 0;
flags &= ZSTR_GET_COPYABLE_CONCAT_PROPERTIES(ptr->str);
ptr++;
} else if (UNEXPECTED(Z_TYPE_P(tmp) == IS_LONG)) {
zend_long val = Z_LVAL_P(tmp);
Expand All @@ -981,12 +981,14 @@ PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return
ptr->str = zval_get_string_func(tmp);
len += ZSTR_LEN(ptr->str);
ptr->lval = 1;
flags &= ZSTR_GET_COPYABLE_CONCAT_PROPERTIES(ptr->str);
ptr++;
}
} ZEND_HASH_FOREACH_END();

/* numelems cannot be 0, we checked above */
str = zend_string_safe_alloc(numelems - 1, ZSTR_LEN(glue), len, 0);
GC_ADD_FLAGS(str, flags);
cptr = ZSTR_VAL(str) + ZSTR_LEN(str);
*cptr = 0;

Expand Down Expand Up @@ -1622,9 +1624,6 @@ PHP_FUNCTION(str_ends_with)
}
/* }}} */

/* {{{ An alias for strstr */
/* }}} */

/* {{{ Finds position of first occurrence of a string within another */
PHP_FUNCTION(strpos)
{
Expand Down
40 changes: 40 additions & 0 deletions ext/zend_test/tests/strings_marked_as_utf8.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@ $string = "\xff";
$string_concat = str_repeat($string, 100);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));

echo "implode:\n";
$arr = ['a', 'b'];
$string_concat = implode('', $arr);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('|', $arr);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', ['c', ...$arr]);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', [...$arr, 'c']);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', ["\xff", ...$arr]);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', [...$arr, "\xff"]);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode("\xff", $arr);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', []);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode("\xff", []);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', ['a']);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode("\xff", ['a']);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
$string_concat = implode('', [1, 1.0, 'a']);
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));

?>
--EXPECT--
Empty strings:
Expand Down Expand Up @@ -159,3 +186,16 @@ bool(true)
str_repeat:
bool(true)
bool(false)
implode:
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)