Skip to content
Open
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
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ PHP NEWS
. Fix OSS-Fuzz #588264417. (ndossche)
. Fixed bug GH-24701 (Use-after-free on typed trait constants during
opcache preload). (sanklamm, lazerg)
. Fixed declare(encoding=...) with zend.multibyte resuming at the wrong
position when the code before it converts to a different length.
(Ilia Alshanetsky)

- Curl:
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)
Expand Down
67 changes: 67 additions & 0 deletions Zend/tests/multibyte/multibyte_encoding_010.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
--TEST--
Zend Multibyte and declare(encoding=...) switching to an encoding of different width
--EXTENSIONS--
mbstring
tokenizer
--INI--
zend.multibyte=1
zend.script_encoding=ISO-8859-1
internal_encoding=UTF-8
--FILE--
<?php
function include_code(string $code) {
static $n = 0;
$file = __DIR__ . '/multibyte_encoding_010_' . ++$n . '.inc';
file_put_contents($file, $code);
return include $file;
}

var_dump(eval("/* \xC3\xA9\xC3\xA9 */ declare(encoding='ISO-8859-1'); return 'caf\xE9';"));
var_dump(eval("declare(encoding='ISO-8859-1'); return 'caf\xE9';"));
var_dump(eval("declare(encoding='UTF-32BE')" . mb_convert_encoding("; return 'ok';", 'UTF-32BE', 'UTF-8')));

$tokens = token_get_all("<?php /*" . str_repeat("\xC3\xA9", 200) . "*/ declare(encoding='ISO-8859-1'); echo 'caf\xE9';", TOKEN_PARSE);
echo implode('|', array_slice(array_map(fn($t) => is_array($t) ? $t[1] : $t, $tokens), -6)), PHP_EOL;

try {
token_get_all('<?php' . str_repeat(' ', 300) . 'declare(encoding="UTF-32"); return 1;', TOKEN_PARSE);
} catch (ParseError $e) {
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
}

var_dump(include_code("<?php /* " . str_repeat("\xE9", 200) . " */ declare(encoding='UTF-8'); return 'caf\xC3\xA9';"));
var_dump(include_code("<?php /* \xA4\xA4 */ declare(encoding='ISO-8859-15'); return '\xA4';"));
var_dump(include_code("\xFF\xFE" . mb_convert_encoding("<?php declare(encoding='UTF-16BE')", 'UTF-16LE', 'UTF-8') . mb_convert_encoding("; return 'ok';", 'UTF-16BE', 'UTF-8')));
var_dump(include_code("\xFF\xFE" . mb_convert_encoding("<?php /* \u{E9}\u{E9} */ declare(encoding='UTF-8')", 'UTF-16LE', 'UTF-8') . "; return 'caf\xC3\xA9';"));

$code = "<?php /* \xE9\xE9\xE9 */ declare(encoding='UTF-8'); return __COMPILER_HALT_OFFSET__; __halt_compiler();data";
var_dump(include_code($code), strpos($code, 'data'));

mb_substitute_character(0xFFFD);
$code = mb_convert_encoding("<?php return __COMPILER_HALT_OFFSET__; __halt_compiler();", 'UTF-16LE', 'UTF-8') . "data";
var_dump(include_code($code), strpos($code, 'data'));

mb_substitute_character(0x1F600);
var_dump(eval("declare(encoding='UTF-16BE')" . mb_convert_encoding("; return 'ok';", 'UTF-16BE', 'UTF-8')));
?>
--CLEAN--
<?php
foreach (glob(__DIR__ . '/multibyte_encoding_010_*.inc') as $file) {
unlink($file);
}
?>
--EXPECT--
string(5) "café"
string(5) "café"
string(2) "ok"
;| |echo| |'café'|;
ParseError: syntax error, unexpected token "??"
string(5) "café"
string(3) "€"
string(2) "ok"
string(5) "café"
int(94)
int(94)
int(114)
int(114)
string(2) "ok"
80 changes: 63 additions & 17 deletions Zend/zend_language_scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -759,23 +759,46 @@ ZEND_API void zend_prepare_string_for_scanning(zval *str, zend_string *filename)
}


static size_t zend_multibyte_script_offset(size_t filtered_offset)
{
size_t low = 0, high = SCNG(script_org_size) + 1;

while (low < high) {
size_t mid = low + (high - low) / 2, length = 0;
unsigned char *p = NULL;

if ((size_t)-1 == SCNG(input_filter)(&p, &length, SCNG(script_org), mid)) {
return (size_t)-1;
}
if (length >= filtered_offset && (!filtered_offset || !memcmp(p, SCNG(yy_start), filtered_offset))) {
high = mid;
} else {
low = mid + 1;
}
efree(p);
}
return low > SCNG(script_org_size) ? (size_t)-1 : low;
}

static size_t zend_multibyte_filtered_length(size_t offset)
{
size_t length = offset;

if (SCNG(input_filter)) {
unsigned char *p = NULL;
if ((size_t)-1 == SCNG(input_filter)(&p, &length, SCNG(script_org), offset)) {
return (size_t)-1;
}
efree(p);
}
return length;
}

ZEND_API size_t zend_get_scanned_file_offset(void)
{
size_t offset = SCNG(yy_cursor) - SCNG(yy_start);
if (SCNG(input_filter)) {
size_t original_offset = offset, length = 0;
do {
unsigned char *p = NULL;
if ((size_t)-1 == SCNG(input_filter)(&p, &length, SCNG(script_org), offset)) {
return (size_t)-1;
}
efree(p);
if (length > original_offset) {
offset--;
} else if (length < original_offset) {
offset++;
}
} while (original_offset != length);
offset = zend_multibyte_script_offset(offset);
}
return offset;
}
Expand Down Expand Up @@ -863,9 +886,27 @@ void highlight_string(zend_string *str, zend_syntax_highlighter_ini *syntax_high

ZEND_API void zend_multibyte_yyinput_again(zend_encoding_filter old_input_filter, const zend_encoding *old_encoding)
{
size_t length;
const zend_encoding *new_encoding = SCNG(script_encoding);
zend_encoding_filter new_input_filter = SCNG(input_filter);
size_t length, offset;
unsigned char *new_yy_start;

if (old_input_filter) {
SCNG(script_encoding) = old_encoding;
SCNG(input_filter) = old_input_filter;
offset = zend_multibyte_script_offset(SCNG(yy_cursor) - SCNG(yy_start));
SCNG(script_encoding) = new_encoding;
SCNG(input_filter) = new_input_filter;
} else {
offset = SCNG(yy_cursor) - SCNG(script_org);
}

if (offset <= SCNG(script_org_size)) {
offset = zend_multibyte_filtered_length(offset);
} else {
offset = (size_t)-1;
}

/* convert and set */
if (!SCNG(input_filter)) {
if (SCNG(script_filtered)) {
Expand All @@ -887,9 +928,14 @@ ZEND_API void zend_multibyte_yyinput_again(zend_encoding_filter old_input_filter
SCNG(script_filtered_size) = length;
}

SCNG(yy_cursor) = new_yy_start + (SCNG(yy_cursor) - SCNG(yy_start));
SCNG(yy_marker) = new_yy_start + (SCNG(yy_marker) - SCNG(yy_start));
SCNG(yy_text) = new_yy_start + (SCNG(yy_text) - SCNG(yy_start));
if (offset > length) {
zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
"encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
}

SCNG(yy_cursor) = new_yy_start + offset;
SCNG(yy_marker) = SCNG(yy_cursor);
SCNG(yy_text) = SCNG(yy_cursor);
SCNG(yy_limit) = new_yy_start + length;

SCNG(yy_start) = new_yy_start;
Expand Down
Loading