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
15 changes: 8 additions & 7 deletions ext/standard/chunk_split.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\BasicBlockHelper;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\JitStringBuiltinArg;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\Variable;
use PHPLLVM\Value;

/** chunk_split() — insert a separator every N bytes (subset of PHP). */
Expand Down Expand Up @@ -43,11 +43,12 @@ public function execute(Frame $frame): void
}
$separator = "\r\n";
if (3 === $argc) {
$sepArg = $frame->calledArgs[2]->resolveIndirect();
if (Variable::TYPE_STRING !== $sepArg->type) {
throw new \LogicException('chunk_split() separator must be a string in this compiler build');
}
$separator = $sepArg->toString();
$separator = VmString::coerceStringBuiltinArg(
$frame->calledArgs[2],
'chunk_split',
2,
'separator'
);
}
$result = VmString::chunkSplit($string, $length, $separator);
if (null === $frame->returnVar) {
Expand All @@ -73,7 +74,7 @@ public function call(Context $context, JITVariable ...$args): Value
JitChunkSplit::emitRuntimeLengthGuard($context, $chunkLen);
}
if ($argc >= 3) {
$separator = $this->jitString($context, $args[2], 'chunk_split() argument #3');
$separator = JitStringBuiltinArg::lower($context, $args[2], 'chunk_split', 2, 'separator');
} else {
$separator = $context->builder->load($context->constantStringFromString("\r\n"));
}
Expand Down
13 changes: 13 additions & 0 deletions test/compliance/cases/stdlib/chunk_split_enum_separator.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
stdlib chunk_split() — backed enum case separator TypeError (#6032, ext/standard/string.c, php-src-strict)
--FILE--
<?php
enum ES: string { case X = '-'; }
try {
chunk_split('abc', 2, ES::X);
echo "uncaught\n";
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
--EXPECT--
chunk_split(): Argument #3 ($separator) must be of type string, ES given
13 changes: 13 additions & 0 deletions test/compliance/cases/stdlib/chunk_split_enum_separator_jit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
stdlib chunk_split() JIT — backed enum case separator TypeError (#6032)
--FILE--
<?php
enum ES: string { case X = '-'; }
try {
chunk_split('abc', 2, ES::X);
echo "uncaught\n";
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
--EXPECT--
chunk_split(): Argument #3 ($separator) must be of type string, ES given
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
// Compile-only (#6032): chunk_split() separator must lower enum-case TypeError guards for AOT.
enum ES: string { case X = '-'; }
try {
chunk_split('abc', 2, ES::X);
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
9 changes: 9 additions & 0 deletions test/repro/issue_6032_chunk_split_enum_separator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
// Issue #6032 — chunk_split() separator enum case must TypeError (ext/standard/string.c)
enum ES: string { case X = '-'; }
try {
chunk_split('abc', 2, ES::X);
echo "uncaught\n";
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
19 changes: 19 additions & 0 deletions test/unit/CountCharsChunkSplitEnumJitCompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,23 @@ enum EI: int { case A = 1; }
$bc
);
}

public function testChunkSplitEnumSeparatorTypeErrorLowering(): void
{
$code = <<<'PHP'
<?php
enum ES: string { case X = '-'; }
chunk_split('abc', 2, ES::X);
PHP;
$runtime = new Runtime();
$block = $runtime->parseAndCompile($code, 'chunk_split_enum_separator_jit_compile.php');
$runtime->jitCompileBlock($block);

$context = $runtime->loadJitContext();
$bc = $context->module->printToString();
$this->assertStringContainsString(
'chunk_split(): Argument #3 ($separator) must be of type string',
$bc
);
}
}