Skip to content

Propagate UTF-8 flag during Rope operations #10915

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 2 commits into from
Mar 26, 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
10 changes: 7 additions & 3 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -3330,8 +3330,6 @@ ZEND_VM_HANDLER(56, ZEND_ROPE_END, TMP, CONST|TMPVAR|CV, NUM)
zend_string **rope;
zval *var, *ret;
uint32_t i;
size_t len = 0;
char *target;

rope = (zend_string**)EX_VAR(opline->op1.var);
if (OP2_TYPE == IS_CONST) {
Expand Down Expand Up @@ -3364,12 +3362,18 @@ ZEND_VM_HANDLER(56, ZEND_ROPE_END, TMP, CONST|TMPVAR|CV, NUM)
}
}
}

size_t len = 0;
uint32_t flags = ZSTR_COPYABLE_CONCAT_PROPERTIES;
for (i = 0; i <= opline->extended_value; i++) {
flags &= ZSTR_GET_COPYABLE_CONCAT_PROPERTIES(rope[i]);
len += ZSTR_LEN(rope[i]);
}
ret = EX_VAR(opline->result.var);
ZVAL_STR(ret, zend_string_alloc(len, 0));
target = Z_STRVAL_P(ret);
GC_ADD_FLAGS(Z_STR_P(ret), flags);

char *target = Z_STRVAL_P(ret);
for (i = 0; i <= opline->extended_value; i++) {
memcpy(target, ZSTR_VAL(rope[i]), ZSTR_LEN(rope[i]));
target += ZSTR_LEN(rope[i]);
Expand Down
30 changes: 21 additions & 9 deletions Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions ext/opcache/jit/zend_jit_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -3104,13 +3104,16 @@ static zend_string* ZEND_FASTCALL zend_jit_rope_end(zend_string **rope, uint32_t
zend_string *ret;
uint32_t i;
size_t len = 0;
char *target;

uint32_t flags = ZSTR_COPYABLE_CONCAT_PROPERTIES;
for (i = 0; i <= count; i++) {
flags &= ZSTR_GET_COPYABLE_CONCAT_PROPERTIES(rope[i]);
len += ZSTR_LEN(rope[i]);
}
ret = zend_string_alloc(len, 0);
target = ZSTR_VAL(ret);
GC_ADD_FLAGS(ret, flags);

char *target = ZSTR_VAL(ret);
for (i = 0; i <= count; i++) {
memcpy(target, ZSTR_VAL(rope[i]), ZSTR_LEN(rope[i]));
target += ZSTR_LEN(rope[i]);
Expand Down
9 changes: 9 additions & 0 deletions ext/zend_test/tests/strings_marked_as_utf8.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ $s = $o . $o;
var_dump($s);
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Rope concat:\n";
$foo = 'f';
$bar = 'b';
$baz = 'a';
$rope = "$foo$bar$baz";
var_dump(zend_test_is_string_marked_as_valid_utf8($rope));

echo "str_repeat:\n";
$string = "a";
$string_concat = str_repeat($string, 100);
Expand Down Expand Up @@ -183,6 +190,8 @@ bool(true)
Concatenation of objects:
string(2) "zz"
bool(true)
Rope concat:
bool(true)
str_repeat:
bool(true)
bool(false)
Expand Down
8 changes: 8 additions & 0 deletions ext/zend_test/tests/strings_not_marked_as_utf8.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ $s = $o . $o;
$s = $s . $non_utf8;
var_dump(zend_test_is_string_marked_as_valid_utf8($s));

echo "Rope concat:\n";
$foo = 'f';
$bar = "\xc3";
$baz = 'a';
$rope = "$foo$bar$baz";
var_dump(zend_test_is_string_marked_as_valid_utf8($rope));
?>
--EXPECT--
Integer cast to string concatenated to invalid UTF-8:
Expand Down Expand Up @@ -129,3 +135,5 @@ Concatenation in loop (compound assignment):
bool(false)
Concatenation of objects:
bool(false)
Rope concat:
bool(false)