[php-src] master: Merge branch 'PHP-8.4' into PHP-8.5
| From: | Weilin Du | Date: | Mon, 07 Sep 2026 16:16:23 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.4' into PHP-8.5 | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141110@lists.php.net to get a copy of this message | ||
Author: Weilin Du (LamentXU123)
Date: 2026-09-08T00:09:15+08:00
Commit: https://github.com/php/php-src/commit/9309a1e27c0ddac7df33a58b6757373ab94c8d19
Raw diff: https://github.com/php/php-src/commit/9309a1e27c0ddac7df33a58b6757373ab94c8d19.diff
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4:
Fix bounds check in multibyte UTF detection (#23527)
Changed paths:
A Zend/tests/multibyte/multibyte_encoding_008.phpt
M NEWS
M Zend/zend_language_scanner.l
Diff:
diff --git a/NEWS b/NEWS
index 346599571294..490c9f559a8c 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,8 @@ PHP NEWS
n_scale. (Ilia Alshanetsky)
- 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-23232 (lone namespace separator asks the autoloader for an
diff --git a/Zend/tests/multibyte/multibyte_encoding_008.phpt
b/Zend/tests/multibyte/multibyte_encoding_008.phpt
new file mode 100644
index 000000000000..69a2aa58369a
--- /dev/null
+++ b/Zend/tests/multibyte/multibyte_encoding_008.phpt
@@ -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
diff --git a/Zend/zend_language_scanner.l b/Zend/zend_language_scanner.l
index 3ecb2f8d0ee4..1696dcf49741 100644
--- a/Zend/zend_language_scanner.l
+++ b/Zend/zend_language_scanner.l
@@ -328,14 +328,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;
}
@@ -345,13 +344,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;
@@ -361,7 +360,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) {