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
34 changes: 6 additions & 28 deletions ext/standard/JitStrReplaceArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPCompiler\JIT\BasicBlockHelper;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\HashTableHelper;
use PHPCompiler\JIT\JitValueBox;
use PHPCompiler\JIT\Variable;
use PHPLLVM\Builder;
use PHPLLVM\Value;
Expand Down Expand Up @@ -82,26 +83,13 @@ private static function buildReplaceHashTable(
$context->builder->branchIf($isSet, $replaceBlock, $skipUnset);

$context->builder->positionAtEnd($replaceBlock);
$entry = self::listEntryAt($context, $src, $srcIdx);
$valueMap = $context->structFieldMap['__value__'];
$typeByte = $context->builder->load(
$context->builder->structGep($entry, $valueMap['type'])
);
$i8 = $context->getTypeFromString('int8');
$isString = $context->builder->icmp(
Builder::INT_EQ,
$typeByte,
$i8->constInt(Variable::TYPE_STRING & 0xff, false)
);
$doReplaceBlock = BasicBlockHelper::append($context, 'str_replace_do_'.$id);
$context->builder->branchIf($isString, $doReplaceBlock, $skipUnset);

$context->builder->positionAtEnd($doReplaceBlock);
// php-src convert_to_string per array subject value (#27165).
$strPtrTy = $context->getTypeFromString('__string__*');
$itemResultSlot = $context->builder->alloca($strPtrTy, 1, 'str_replace_arr_item_'.$id);
$subject = $context->builder->call(
$context->lookupFunction('__value__readString'),
$entry
$entryBox = HashTableHelper::readIndexedToValueBox($context, $src, $srcIdx);
$subject = (new strval())->valueToString(
$context,
JitValueBox::pointer($context, $entryBox->value)
);
$itemCountSlot = null;
if (null !== $countSlot) {
Expand Down Expand Up @@ -158,14 +146,4 @@ private static function buildReplaceHashTable(

return $phi;
}

private static function listEntryAt(Context $context, Value $ht, Value $index): Value
{
$map = $context->structFieldMap['__hashtable__'];
$values = $context->builder->load(
$context->builder->structGep($ht, $map['values'])
);

return $context->builder->inBoundsGep($values, $index);
}
}
8 changes: 2 additions & 6 deletions ext/standard/str_ireplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,12 @@ public function execute(Frame $frame): void
$ht = new HashTable();
$totalCount = 0;
foreach ($subjectVar->toArray()->iterateKeyed(true) as [$key, $value]) {
if (Variable::TYPE_STRING !== $value->type) {
throw new \LogicException(
'str_ireplace() array subject values must be strings in this compiler build'
);
}
// php-src php_str_replace_array: convert_to_string per array subject (#27165).
$elemCount = 0;
$replaced = self::replaceWithNeedles(
$searchNeedles,
$replaceOperand,
$value->toString(),
$value->resolveIndirect()->toString(null, $frame),
$elemCount
);
$totalCount += $elemCount;
Expand Down
8 changes: 2 additions & 6 deletions ext/standard/str_replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,12 @@ public function execute(Frame $frame): void
$ht = new HashTable();
$totalCount = 0;
foreach ($subjectVar->toArray()->iterateKeyed(true) as [$key, $value]) {
if (Variable::TYPE_STRING !== $value->type) {
throw new \LogicException(
'str_replace() array subject values must be strings in this compiler build'
);
}
// php-src php_str_replace_array: convert_to_string per array subject (#27165).
$elemCount = 0;
$replaced = self::replaceWithNeedles(
$searchNeedles,
$replaceOperand,
$value->toString(),
$value->resolveIndirect()->toString(null, $frame),
$elemCount
);
$totalCount += $elemCount;
Expand Down
18 changes: 18 additions & 0 deletions test/compliance/cases/stdlib/str_replace_array_value_coerce.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
stdlib str_replace/str_ireplace coerce non-string array subject values (#27165, ext/standard/string.c)
--FILE--
<?php
var_export(str_replace('1', 'X', [12, '13']));
echo "\n";
var_export(str_ireplace('a', 'X', ['a', 12, 'A']));
echo "\n";
--EXPECT--
array (
0 => 'X2',
1 => 'X3',
)
array (
0 => 'X',
1 => '12',
2 => 'X',
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
stdlib str_replace/str_ireplace coerce non-string array subject values JIT (#27165, ext/standard/string.c)
--FILE--
<?php
var_export(str_replace('1', 'X', [12, '13']));
echo "\n";
var_export(str_ireplace('a', 'X', ['a', 12, 'A']));
echo "\n";
--EXPECT--
array (
0 => 'X2',
1 => 'X3',
)
array (
0 => 'X',
1 => '12',
2 => 'X',
)
5 changes: 5 additions & 0 deletions test/repro/issue_27165_str_replace_array_value_coerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
var_export(str_replace('1', 'X', [12, '13']));
echo "\n";
var_export(str_ireplace('a', 'X', ['a', 12, 'A']));
echo "\n";
Loading