Skip to content
Closed
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ PHP NEWS
?? ??? ????, PHP 8.4.26

- Core:
. Fixed out-of-bounds reads during automatic UTF-16/32 encoding detection.
(Yudai Takada)
. Fixed bug GH-15375 (Nested "yield from" skips items after a valid() or
next() call on the inner generator). (iliaal)
. Fixed bug GH-23301 (Nested "yield from" yields a value twice when the
Expand Down
20 changes: 20 additions & 0 deletions Zend/tests/multibyte/multibyte_encoding_008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Zend Multibyte does not read past the script during UTF-16 detection
--EXTENSIONS--
mbstring
--INI--
zend.multibyte=1
internal_encoding=UTF-8
--FILE--
<?php
$filename = __DIR__ . '/multibyte_encoding_008.tmp.php';
file_put_contents($filename, "<\0?\0p\0h\0p\0");
include $filename;
echo "Done\n";
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/multibyte_encoding_008.tmp.php');
?>
--EXPECT--
Done
17 changes: 8 additions & 9 deletions Zend/zend_language_scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,13 @@ ZEND_API zend_result zend_lex_tstring(zval *zv, unsigned char *ident)
static const zend_encoding *zend_multibyte_detect_utf_encoding(const unsigned char *script, size_t script_size)
{
const unsigned char *p;
size_t offset = 0;
int wchar_size = 2;
int le = 0;

/* utf-16 or utf-32? */
p = script;
assert(p >= script);
while ((size_t)(p-script) < script_size) {
p = memchr(p, 0, script_size-(p-script)-2);
while (offset < script_size && script_size - offset > 2) {
p = memchr(script + offset, 0, script_size - offset - 2);
if (!p) {
break;
}
Expand All @@ -344,13 +343,13 @@ static const zend_encoding *zend_multibyte_detect_utf_encoding(const unsigned ch
}

/* searching for UTF-32 specific byte orders, so this will do */
p += 4;
offset = p - script + 4;
}

/* BE or LE? */
p = script;
assert(p >= script);
while ((size_t)(p-script) < script_size) {
offset = 0;
while (script_size - offset >= (size_t) wchar_size) {
p = script + offset;
if (*p == '\0' && *(p+wchar_size-1) != '\0') {
/* BE */
le = 0;
Expand All @@ -360,7 +359,7 @@ static const zend_encoding *zend_multibyte_detect_utf_encoding(const unsigned ch
le = 1;
break;
}
p += wchar_size;
offset += wchar_size;
}

if (wchar_size == 2) {
Expand Down
Loading