[php-src] master: Merge branch 'PHP-8.5'

From: Date: Mon, 07 Sep 2026 16:16:26 +0000
Subject: [php-src] master: Merge branch 'PHP-8.5'
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-141111@lists.php.net to get a copy of this message
Author: Weilin Du (LamentXU123) Date: 2026-09-08T00:11:02+08:00 Commit: https://github.com/php/php-src/commit/de748772668634dd984c9c295e5b82bde6aa8254 Raw diff: https://github.com/php/php-src/commit/de748772668634dd984c9c295e5b82bde6aa8254.diff Merge branch 'PHP-8.5' * PHP-8.5: 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 5c6f6e8db9e3..c1bf79e07fba 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) . Calling is_a() or is_subclass_of() with a string as the first argument when $allow_string is false is now deprecated. (Daniel Scherzer) . 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 bf6bbe7f9d90..60143bc800fe 100644 --- a/Zend/zend_language_scanner.l +++ b/Zend/zend_language_scanner.l @@ -332,14 +332,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; } @@ -349,13 +348,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; @@ -365,7 +364,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) {

« previous php.cvs (#141111) next »