Skip to content
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
28 changes: 25 additions & 3 deletions ext/standard/VmPreg.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,19 @@ private static function pregReplaceArrayPatterns(
}
$elemCount = 0;
$replaced = self::pregReplaceArrayPatterns($pattern, $replacements, $item, $limit, $elemCount);
if (false === $replaced || null === $replaced) {
return $replaced;
if (false === $replaced) {
return false;
}
if (null === $replaced) {
if (StdlibConstants::PREG_BAD_UTF8_ERROR === self::lastError()) {
if (null !== $count) {
$count = $totalCount;
}

return $out;
}

return null;
}
$out[$key] = $replaced;
$totalCount += $elemCount;
Expand All @@ -348,9 +359,20 @@ private static function pregReplaceArrayPatterns(
$stepCount = 0;
$step = VmPregNative::pregReplace($pat, $repl, $result, $limit, $stepCount);
self::syncLastErrorFromNative();
if (false === $step || null === $step) {
if (false === $step) {
return false;
}
if (null === $step) {
if (StdlibConstants::PREG_BAD_UTF8_ERROR === self::lastError()) {
if (null !== $count) {
$count = $totalCount;
}

return $result;
}

return null;
}
$result = $step;
$totalCount += $stepCount;
}
Expand Down
5 changes: 4 additions & 1 deletion ext/standard/VmPregPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/
final class VmPregPattern
{
/** PCRE2_UTF — PHP /u pattern modifier (ext/pcre/php_pcre.c). */
public const PCRE2_UTF = 0x00080000;

/**
* Zend ext/pcre/php_pcre.c delimiter/compile failure text (issue #12083).
*/
Expand Down Expand Up @@ -124,7 +127,7 @@ public static function parsePhpPattern(string $pattern): ?array
'A' => 0x80000000,
'D' => 0x00000010,
'U' => 0x00040000,
'u' => 0x00080000,
'u' => self::PCRE2_UTF,
default => 0,
};
}
Expand Down
82 changes: 68 additions & 14 deletions ext/standard/VmPregPure.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ public static function pregMatch(
return false;
}
[$regex, $opts] = $parsed;
$compiled = self::compile($pattern);
if (null === $compiled) {
return false;
}

$normalizedOffset = self::normalizeMatchSubjectOffset($offset, \strlen($subject));
if (false === $normalizedOffset) {
self::$lastError = StdlibConstants::PREG_INTERNAL_ERROR;

return false;
}
if (!self::ensureUtf8SubjectForOpts($subject, $opts, $normalizedOffset)) {
return false;
}
$compiled = self::compile($pattern);
if (null === $compiled) {
return false;
}

$ovector = self::engineMatch(
$compiled['ast'],
Expand Down Expand Up @@ -100,18 +102,20 @@ public static function pregMatchAll(
return false;
}
[$regex, $opts] = $parsed;
$compiled = self::compile($pattern);
if (null === $compiled) {
return false;
}

$subjectLen = \strlen($subject);
$normalizedOffset = self::normalizeMatchSubjectOffset($offset, $subjectLen);
if (false === $normalizedOffset) {
self::$lastError = StdlibConstants::PREG_INTERNAL_ERROR;

return false;
}
if (!self::ensureUtf8SubjectForOpts($subject, $opts, $normalizedOffset)) {
return false;
}
$compiled = self::compile($pattern);
if (null === $compiled) {
return false;
}

$start = $normalizedOffset;
$count = 0;
Expand Down Expand Up @@ -185,8 +189,19 @@ public static function pregReplace(
}
$elemCount = 0;
$replaced = self::pregReplaceString($pattern, $replacement, $item, $limit, $elemCount);
if (false === $replaced || null === $replaced) {
return $replaced;
if (false === $replaced) {
return false;
}
if (null === $replaced) {
if (StdlibConstants::PREG_BAD_UTF8_ERROR === self::$lastError) {
if (null !== $count) {
$count = $totalCount;
}

return $out;
}

return null;
}
$out[$key] = $replaced;
$totalCount += $elemCount;
Expand Down Expand Up @@ -214,14 +229,21 @@ public static function pregSplit(string $pattern, string $subject, int $limit =
}
[$regex, $opts] = $parsed;
if ('' === $regex) {
if (!self::ensureUtf8SubjectForOpts($subject, $opts, 0)) {
return false;
}

return self::pregSplitEmptyPattern(
$subject,
$limit,
$flags,
0 !== ($opts & 0x00080000)
0 !== ($opts & VmPregPattern::PCRE2_UTF)
);
}

if (!self::ensureUtf8SubjectForOpts($subject, $opts, 0)) {
return false;
}
$compiled = self::compile($pattern);
if (null === $compiled) {
return false;
Expand Down Expand Up @@ -325,7 +347,7 @@ private static function splitAdvanceUnit(string $subject, int $byteOffset, int $
if ($byteOffset >= \strlen($subject)) {
return 0;
}
if (0 !== ($opts & 0x00080000)) {
if (0 !== ($opts & VmPregPattern::PCRE2_UTF)) {
$charIndex = 0;
$pos = 0;
$charLen = VmString::utf8CharLength($subject);
Expand Down Expand Up @@ -407,6 +429,9 @@ private static function pregReplaceString(
$emptyParsed = PregEmptyPatternReplace::parseEmptyPattern($pattern);
if (null !== $emptyParsed) {
[, $opts] = $emptyParsed;
if (!self::ensureUtf8SubjectForOpts($subject, $opts, 0)) {
return null;
}
self::$lastError = 0;
$replacements = 0;
$result = PregEmptyPatternReplace::replace(
Expand All @@ -430,6 +455,9 @@ private static function pregReplaceString(
return null;
}
[$regex, $opts] = $parsed;
if (!self::ensureUtf8SubjectForOpts($subject, $opts, 0)) {
return null;
}
$compiled = self::compile($pattern);
if (null === $compiled) {
self::$lastError = 1;
Expand Down Expand Up @@ -550,6 +578,32 @@ private static function normalizeMatchSubjectOffset(int $offset, int $subjectLen
return $offset;
}

/**
* php-src ext/pcre/php_pcre.c — reject malformed UTF-8 subjects when /u is set.
*/
private static function ensureUtf8SubjectForOpts(string $subject, int $opts, int $offset): bool
{
if (0 === ($opts & VmPregPattern::PCRE2_UTF)) {
return true;
}
if (!VmString::isValidUtf8($subject)) {
self::$lastError = StdlibConstants::PREG_BAD_UTF8_ERROR;

return false;
}
$subjectLen = \strlen($subject);
if ($offset > 0 && $offset < $subjectLen) {
$byte = \ord($subject[$offset]);
if (($byte & 0xC0) === 0x80) {
self::$lastError = StdlibConstants::PREG_BAD_UTF8_OFFSET_ERROR;

return false;
}
}

return true;
}

/**
* @param list<int> $ovector
* @param array<string, int> $groupNameToIndex
Expand Down
3 changes: 3 additions & 0 deletions ext/standard/preg_grep.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public function execute(Frame $frame): void
self::rejectNonStringHaystackValue($value);
$match = VmPreg::pregMatch($pattern, $value->toString());
if (false === $match) {
if (StdlibConstants::PREG_BAD_UTF8_ERROR === VmPreg::lastError()) {
continue;
}
if (null !== $frame->returnVar) {
$frame->returnVar->bool(false);
}
Expand Down
32 changes: 32 additions & 0 deletions test/compliance/cases/pcre/preg_utf8_invalid_subject.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
pcre preg_* /u modifier rejects invalid UTF-8 subject (#17503, ext/pcre/php_pcre.c)
--FILE--
<?php
$bad = "\xFF";

preg_match('//u', $bad, $m, 0, 0);
echo preg_match('//u', $bad) === false ? 'match_fail' : 'match_ok', "\n";
echo preg_last_error(), "\n";

preg_split('//u', $bad);
echo preg_split('//u', $bad) === false ? 'split_fail' : 'split_ok', "\n";
echo preg_last_error(), "\n";

preg_replace('//u', 'x', $bad);
echo preg_replace('//u', 'x', $bad) === null ? 'replace_null' : 'replace_ok', "\n";
echo preg_last_error(), "\n";

var_export(preg_grep('//u', ['a', $bad]));
echo "\n";
echo preg_last_error(), "\n";
--EXPECT--
match_fail
4
split_fail
4
replace_null
4
array (
0 => 'a',
)
4
11 changes: 11 additions & 0 deletions test/repro/maintainer_gap_preg_match_invalid_utf8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

/** Issue #17503 — preg_match(/u) on invalid UTF-8 (ext/pcre/php_pcre.c). */
$bad = "\xFF";

var_export(preg_match('//u', $bad));
echo "\n";
var_export(preg_last_error());
echo "\n";
11 changes: 11 additions & 0 deletions test/repro/maintainer_gap_preg_split_invalid_utf8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

/** Issue #17503 — preg_split(/u) on invalid UTF-8 (ext/pcre/php_pcre.c). */
$bad = "\xFF";

var_export(preg_split('//u', $bad));
echo "\n";
var_export(preg_last_error());
echo "\n";
45 changes: 45 additions & 0 deletions test/unit/VmPregUtf8SubjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\Test\Unit;

use PHPCompiler\ext\standard\PregJitHelper;
use PHPCompiler\ext\standard\StdlibConstants;
use PHPCompiler\ext\standard\VmPregNative;
use PHPCompiler\ext\standard\VmPregPure;
use PHPUnit\Framework\TestCase;

/** preg_* /u rejects malformed UTF-8 subjects (#17503, ext/pcre/php_pcre.c). */
final class VmPregUtf8SubjectTest extends TestCase
{
private const BAD = "\xFF";

public function testPregMatchInvalidUtf8SetsBadUtf8Error(): void
{
VmPregPure::setLastError(0);
$this->assertFalse(VmPregPure::pregMatch('//u', self::BAD));
$this->assertSame(StdlibConstants::PREG_BAD_UTF8_ERROR, VmPregPure::lastError());
}

public function testPregSplitInvalidUtf8SetsBadUtf8Error(): void
{
VmPregPure::setLastError(0);
$this->assertFalse(VmPregPure::pregSplit('//u', self::BAD));
$this->assertSame(StdlibConstants::PREG_BAD_UTF8_ERROR, VmPregPure::lastError());
}

public function testPregReplaceInvalidUtf8ReturnsNull(): void
{
VmPregPure::setLastError(0);
$this->assertNull(VmPregPure::pregReplace('//u', 'x', self::BAD));
$this->assertSame(StdlibConstants::PREG_BAD_UTF8_ERROR, VmPregPure::lastError());
}

public function testJitHelperPropagatesBadUtf8Error(): void
{
VmPregNative::setLastError(0);
$this->assertSame(-1, PregJitHelper::matchArgv('//u', self::BAD));
$this->assertSame(StdlibConstants::PREG_BAD_UTF8_ERROR, PregJitHelper::lastError());
}
}