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
31 changes: 30 additions & 1 deletion ext/standard/VmString.php
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,7 @@ public static function strspn(string $str, string $mask, int $offset = 0, ?int $

/**
* PHP 8.4 (GH-12592): empty $mask returns full segment byte length (not NUL-terminated walk).
* PROFILE<8.4: empty $mask stops at the first embedded NUL like classic php-src / C strcspn (#27716).
*/
public static function strcspn(string $str, string $mask, int $offset = 0, ?int $length = null): int
{
Expand All @@ -1735,7 +1736,19 @@ public static function strcspn(string $str, string $mask, int $offset = 0, ?int
return 0;
}
if ('' === $mask) {
return $len;
if (!self::strcspnEmptyMaskStopsAtNul()) {
return $len;
}
// PROFILE≤8.3 empty mask — stop at embedded NUL within the segment (#27716).
$count = 0;
for ($i = $start; $i < $start + $len; ++$i) {
if ("\0" === $str[$i]) {
break;
}
++$count;
}

return $count;
}
$mlen = self::byteLength($mask);
$count = 0;
Expand All @@ -1749,6 +1762,22 @@ public static function strcspn(string $str, string $mask, int $offset = 0, ?int
return $count;
}

/**
* Empty $characters + embedded NUL (#27716 / GH-12592).
*
* Explicit PROFILE&lt;8.4 matches Zend 8.2 (stop at NUL). Unset PROFILE and PROFILE≥8.4 keep
* the binary-safe full-segment length from #7088 (`strcspn_empty_characters_84`).
*/
public static function strcspnEmptyMaskStopsAtNul(): bool
{
$raw = getenv('PHP_COMPILER_PROFILE');
if (!\is_string($raw) || '' === trim($raw)) {
return false;
}

return version_compare(CompilerVersion::languageProfileVersion(), '8.4.0', '<');
}

public static function strpbrk(string $str, string $mask) {
if ('' === $mask) {
throw new \ValueError('strpbrk(): Argument #2 ($characters) must be a non-empty string');
Expand Down
45 changes: 44 additions & 1 deletion lib/JIT/Builtin/StringStrspn.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,50 @@ private static function emitExtendedBody(Context $context): void
$context->builder->branchIf($maskEmptyCspn, $cspnEmptyMaskBb, $loopSetupBb);

$context->builder->positionAtEnd($cspnEmptyMaskBb);
$context->builder->returnValue($segLen);
// Unset PROFILE / PROFILE≥8.4 (GH-12592, #7088): empty mask → full segment length.
// Explicit PROFILE&lt;8.4 (#27716): stop at first embedded NUL like Zend 8.2.
if (!\PHPCompiler\ext\standard\VmString::strcspnEmptyMaskStopsAtNul()) {
$context->builder->returnValue($segLen);
} else {
$nulCountSlot = $context->builder->alloca($i64, 1, 'cspn_empty_nul_cnt');
$nulIdxSlot = $context->builder->alloca($i64, 1, 'cspn_empty_nul_i');
$context->builder->store($zero, $nulCountSlot);
$context->builder->store($start, $nulIdxSlot);
$nulEnd = $context->builder->add($start, $segLen);
$nulHead = $fn->appendBasicBlock('cspn_empty_nul_head');
$nulBody = $fn->appendBasicBlock('cspn_empty_nul_body');
$nulInc = $fn->appendBasicBlock('cspn_empty_nul_inc');
$nulDone = $fn->appendBasicBlock('cspn_empty_nul_done');
$context->builder->branch($nulHead);

$context->builder->positionAtEnd($nulHead);
$nulI = $context->builder->load($nulIdxSlot);
$nulAtEnd = $context->builder->icmp(Builder::INT_SGE, $nulI, $nulEnd);
$context->builder->branchIf($nulAtEnd, $nulDone, $nulBody);

$context->builder->positionAtEnd($nulBody);
$nulByte = $context->builder->load($context->builder->gep($strData, $nulI));
$isNul = $context->builder->icmp(
Builder::INT_EQ,
$nulByte,
$i8->constInt(0, false)
);
$context->builder->branchIf($isNul, $nulDone, $nulInc);

$context->builder->positionAtEnd($nulInc);
$context->builder->store(
$context->builder->add($context->builder->load($nulCountSlot), $one),
$nulCountSlot
);
$context->builder->store(
$context->builder->add($nulI, $one),
$nulIdxSlot
);
$context->builder->branch($nulHead);

$context->builder->positionAtEnd($nulDone);
$context->builder->returnValue($context->builder->load($nulCountSlot));
}

$context->builder->positionAtEnd($loopSetupBb);
$iSlot = $context->builder->alloca($i64, 1);
Expand Down
13 changes: 13 additions & 0 deletions test/compliance/cases/stdlib/strcspn_empty_nul_profile82.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
stdlib strcspn() empty $characters + NUL — PROFILE=8.2 stops at NUL (#27716, GH-12592 inverse)
--ENV--
PHP_COMPILER_PROFILE=8.2
--FILE--
<?php
echo strcspn("ab\0c", ""), "\n";
echo strcspn("abc", ""), "\n";
echo strcspn("ab\0c", "", 1), "\n";
--EXPECT--
2
3
1
13 changes: 13 additions & 0 deletions test/compliance/cases/stdlib/strcspn_empty_nul_profile82_jit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
stdlib strcspn() empty $characters + NUL JIT — PROFILE=8.2 stops at NUL (#27716)
--ENV--
PHP_COMPILER_PROFILE=8.2
--FILE--
<?php
echo strcspn("ab\0c", ""), "\n";
echo strcspn("abc", ""), "\n";
echo strcspn("ab\0c", "", 1), "\n";
--EXPECT--
2
3
1
11 changes: 11 additions & 0 deletions test/compliance/cases/stdlib/strcspn_empty_nul_profile84.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
stdlib strcspn() empty $characters + NUL — PROFILE=8.4 full length (#27716, GH-12592)
--ENV--
PHP_COMPILER_PROFILE=8.4
--FILE--
<?php
echo strcspn("ab\0c", ""), "\n";
echo strcspn("abc", ""), "\n";
--EXPECT--
4
3
3 changes: 3 additions & 0 deletions test/repro/issue_27716_strcspn_nul_profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
echo strcspn("ab\0c", ""), "\n";
echo strcspn("abc", ""), "\n";
Loading