Skip to content

Commit 3821938

Browse files
authored
Imply UTF8 validity in implode function (#10780)
Sets the UTF-8 valid flag if all parts are valid, or numeric (which are valid UTF-8 by definition). * remove unuseful comments * Imply UTF8 validity in implode function * revert zend_string_dup change
1 parent 67b5e6b commit 3821938

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

ext/standard/string.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -928,9 +928,6 @@ PHP_FUNCTION(explode)
928928
}
929929
/* }}} */
930930

931-
/* {{{ An alias for implode */
932-
/* }}} */
933-
934931
/* {{{ php_implode */
935932
PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return_value)
936933
{
@@ -958,11 +955,14 @@ PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return
958955

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

958+
uint32_t flags = ZSTR_GET_COPYABLE_CONCAT_PROPERTIES(glue);
959+
961960
ZEND_HASH_FOREACH_VAL(pieces, tmp) {
962961
if (EXPECTED(Z_TYPE_P(tmp) == IS_STRING)) {
963962
ptr->str = Z_STR_P(tmp);
964963
len += ZSTR_LEN(ptr->str);
965964
ptr->lval = 0;
965+
flags &= ZSTR_GET_COPYABLE_CONCAT_PROPERTIES(ptr->str);
966966
ptr++;
967967
} else if (UNEXPECTED(Z_TYPE_P(tmp) == IS_LONG)) {
968968
zend_long val = Z_LVAL_P(tmp);
@@ -981,12 +981,14 @@ PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return
981981
ptr->str = zval_get_string_func(tmp);
982982
len += ZSTR_LEN(ptr->str);
983983
ptr->lval = 1;
984+
flags &= ZSTR_GET_COPYABLE_CONCAT_PROPERTIES(ptr->str);
984985
ptr++;
985986
}
986987
} ZEND_HASH_FOREACH_END();
987988

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

@@ -1622,9 +1624,6 @@ PHP_FUNCTION(str_ends_with)
16221624
}
16231625
/* }}} */
16241626

1625-
/* {{{ An alias for strstr */
1626-
/* }}} */
1627-
16281627
/* {{{ Finds position of first occurrence of a string within another */
16291628
PHP_FUNCTION(strpos)
16301629
{

ext/zend_test/tests/strings_marked_as_utf8.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,33 @@ $string = "\xff";
115115
$string_concat = str_repeat($string, 100);
116116
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
117117

118+
echo "implode:\n";
119+
$arr = ['a', 'b'];
120+
$string_concat = implode('', $arr);
121+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
122+
$string_concat = implode('|', $arr);
123+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
124+
$string_concat = implode('', ['c', ...$arr]);
125+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
126+
$string_concat = implode('', [...$arr, 'c']);
127+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
128+
$string_concat = implode('', ["\xff", ...$arr]);
129+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
130+
$string_concat = implode('', [...$arr, "\xff"]);
131+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
132+
$string_concat = implode("\xff", $arr);
133+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
134+
$string_concat = implode('', []);
135+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
136+
$string_concat = implode("\xff", []);
137+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
138+
$string_concat = implode('', ['a']);
139+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
140+
$string_concat = implode("\xff", ['a']);
141+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
142+
$string_concat = implode('', [1, 1.0, 'a']);
143+
var_dump(zend_test_is_string_marked_as_valid_utf8($string_concat));
144+
118145
?>
119146
--EXPECT--
120147
Empty strings:
@@ -159,3 +186,16 @@ bool(true)
159186
str_repeat:
160187
bool(true)
161188
bool(false)
189+
implode:
190+
bool(true)
191+
bool(true)
192+
bool(true)
193+
bool(true)
194+
bool(false)
195+
bool(false)
196+
bool(false)
197+
bool(true)
198+
bool(true)
199+
bool(true)
200+
bool(true)
201+
bool(true)

0 commit comments

Comments
 (0)