Skip to content

Commit 736e0cc

Browse files
ondrejmirtesclaude
andcommitted
Check the variadic Type arguments of the native TypeCombinator entry points
union(), doUnion(), intersect() and doIntersect() parsed their variadic arguments as any values and handed them straight to the implementation, which crashed on a scalar (TypeCombinator::doUnion('x')) and failed later with an unrelated Error on a non-Type object. The twins' `Type ...$types` parameter rejects both with a TypeError naming the argument; the glue now raises the same TypeError before calling the implementation. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent ca33650 commit 736e0cc

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎turbo-ext/src/TypeCombinator.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3985,6 +3985,21 @@ zv::Val pt_type_combinator_call_spread(const char *lcname, size_t len, HashTable
39853985

39863986
/* {{{ engine ABI glue: parameter parsing + registration */
39873987

3988+
/* the twin's `Type ...$types` parameter check for the variadic entry
3989+
* points; false with the engine's TypeError pending */
3990+
static bool requireTypeArguments(uint32_t count, zval *types)
3991+
{
3992+
for (uint32_t i = 0; i < count; i++) {
3993+
bool isType;
3994+
if (UNEXPECTED(!pt_type_instanceof(&types[i], PT_CLASS_TYPE, isType))) return false;
3995+
if (UNEXPECTED(!isType)) {
3996+
zend_argument_type_error(i + 1, "must be of type %s, %s given", ptcls::type, zend_zval_value_name(&types[i]));
3997+
return false;
3998+
}
3999+
}
4000+
return true;
4001+
}
4002+
39884003
void pt_register_type_combinator()
39894004
{
39904005

@@ -4029,6 +4044,7 @@ void pt_register_type_combinator()
40294044
ZEND_PARSE_PARAMETERS_START(0, -1)
40304045
Z_PARAM_VARIADIC('*', types, count)
40314046
ZEND_PARSE_PARAMETERS_END();
4047+
if (UNEXPECTED(!requireTypeArguments(count, types))) RETURN_THROWS();
40324048
PT_RETURN_VAL(TypeCombinator::union_(count, types));
40334049
});
40344050
cls.method(sigs::doUnion, [](INTERNAL_FUNCTION_PARAMETERS) {
@@ -4037,6 +4053,7 @@ void pt_register_type_combinator()
40374053
ZEND_PARSE_PARAMETERS_START(0, -1)
40384054
Z_PARAM_VARIADIC('*', types, count)
40394055
ZEND_PARSE_PARAMETERS_END();
4056+
if (UNEXPECTED(!requireTypeArguments(count, types))) RETURN_THROWS();
40404057
PT_RETURN_VAL(TypeCombinator::doUnion(count, types));
40414058
});
40424059
cls.method(sigs::countConstantArrayValueTypes, [](INTERNAL_FUNCTION_PARAMETERS) {
@@ -4052,6 +4069,7 @@ void pt_register_type_combinator()
40524069
ZEND_PARSE_PARAMETERS_START(0, -1)
40534070
Z_PARAM_VARIADIC('*', types, count)
40544071
ZEND_PARSE_PARAMETERS_END();
4072+
if (UNEXPECTED(!requireTypeArguments(count, types))) RETURN_THROWS();
40554073
PT_RETURN_VAL(TypeCombinator::intersect(count, types));
40564074
});
40574075
cls.method(sigs::doIntersect, [](INTERNAL_FUNCTION_PARAMETERS) {
@@ -4060,6 +4078,7 @@ void pt_register_type_combinator()
40604078
ZEND_PARSE_PARAMETERS_START(0, -1)
40614079
Z_PARAM_VARIADIC('*', types, count)
40624080
ZEND_PARSE_PARAMETERS_END();
4081+
if (UNEXPECTED(!requireTypeArguments(count, types))) RETURN_THROWS();
40634082
PT_RETURN_VAL(TypeCombinator::doIntersect(count, types));
40644083
});
40654084
cls.method(sigs::removeFalsey, [](INTERNAL_FUNCTION_PARAMETERS) {

‎turbo-ext/tests/type-family.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8371,6 +8371,13 @@ public function getTypes(): array
83718371
};
83728372
$r['benevolent overridden getTypes getOffsetValueType'] = $misuse(static fn () => $misuseBenevolent->getOffsetValueType(new \PHPStan\Type\IntegerType()), true);
83738373

8374+
// TypeCombinator's `Type ...$types` entry points
8375+
foreach (['union', 'doUnion', 'intersect', 'doIntersect'] as $method) {
8376+
foreach (['string' => ['x'], 'int second' => [new \PHPStan\Type\IntegerType(), 1], 'null' => [null, new \PHPStan\Type\IntegerType()], 'object' => [new \stdClass(), new \PHPStan\Type\IntegerType()], 'object second' => [new \PHPStan\Type\IntegerType(), new \stdClass()]] as $argsName => $args) {
8377+
$r["combinator $method $argsName"] = $misuse(static fn () => \PHPStan\Type\TypeCombinator::$method(...$args));
8378+
}
8379+
}
8380+
83748381
foreach ($r as $key => $value) {
83758382
$observations["misuse $key"] = $value;
83768383
}

0 commit comments

Comments
 (0)