Skip to content

Commit ca33650

Browse files
ondrejmirtesclaude
andcommitted
Reject non-object members when constructing the native compound types
The twins' IntersectionType and UnionType constructors store their members unchecked, and a member that is not a Type fails at its first use (an Error, or a TypeError from a Type-typed closure). The native code dereferenced such a member as an object in nearly every method of IntersectionType, in several of UnionType and BenevolentUnionType, and in every port iterating getTypes(), crashing the process. Instead of checking each of those sites, the native constructors now reject a non-object member with a TypeError — the invariant the native code already relies on — which UnionType's constructor gets for free in the loop it already runs over the members. BenevolentUnionType's getOffsetValueType() additionally checks the members it gets from a possibly overridden getTypes() and raises the twin's Error. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 716b3c2 commit ca33650

4 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎turbo-ext/src/BenevolentUnionType.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,14 @@ class BenevolentUnionType
191191
if (UNEXPECTED(types.isUndef())) return zv::Val();
192192
zv::Arr valueTypes = zv::Arr::create(0);
193193
for (zv::ArrayEntry entry : zv::ArrRef(types.raw())) {
194-
zv::Val valueType = callType(entry.value().deref().asObject(), PT_LC("getoffsetvaluetype"), 1, offsetType);
194+
/* the members come from $this->getTypes(), which a subclass may
195+
* override: a non-object fails like the twin's method call */
196+
zval *innerType = entry.value().deref().raw();
197+
if (UNEXPECTED(Z_TYPE_P(innerType) != IS_OBJECT)) {
198+
zend_throw_error(NULL, "Call to a member function getOffsetValueType() on %s", zend_zval_value_name(innerType));
199+
return zv::Val();
200+
}
201+
zv::Val valueType = callType(Z_OBJ_P(innerType), PT_LC("getoffsetvaluetype"), 1, offsetType);
195202
if (UNEXPECTED(valueType.isUndef())) return zv::Val();
196203
bool isError;
197204
if (UNEXPECTED(!pt_type_instanceof_ce(valueType.raw(), pt_ce_error_type, isError))) return zv::Val();

‎turbo-ext/src/IntersectionType.cpp‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,14 @@ class IntersectionType
342342
explicit IntersectionType(zend_object *self) : self(self) {}
343343

344344
/* __construct(private array $types): fewer than two members throw;
345-
* false = pending exception */
345+
* false = pending exception.
346+
*
347+
* A member that is not an object is rejected here with a TypeError: the
348+
* twin stores it unchecked and fails at its first use (an Error or,
349+
* through its `Type`-typed closures, a TypeError), but the native code
350+
* reading the members — here and in every port iterating getTypes() —
351+
* relies on them being objects, so one check at construction keeps the
352+
* whole family from dereferencing a scalar */
346353
[[nodiscard]] bool construct(zval *typesArg)
347354
{
348355
zval *typesSlot = OBJ_PROP_NUM(self, slots::types);
@@ -359,6 +366,13 @@ class IntersectionType
359366
throwCannotCreate(typesArg);
360367
return false;
361368
}
369+
for (zv::ArrayEntry entry : zv::ArrRef(typesArg)) {
370+
zval *type = entry.value().deref().raw();
371+
if (UNEXPECTED(Z_TYPE_P(type) != IS_OBJECT)) {
372+
zend_type_error("phpstan_turbo: %s::__construct(): every member must be a %s, %s given", ZSTR_VAL(pt_ce_intersection_type->name), ptcls::type, zend_zval_value_name(type));
373+
return false;
374+
}
375+
}
362376
return true;
363377
}
364378

‎turbo-ext/src/UnionType.cpp‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,15 @@ class UnionType
341341
}
342342
for (zv::ArrayEntry entry : zv::ArrRef(typesArg)) {
343343
zval *type = entry.value().deref().raw();
344-
if (Z_TYPE_P(type) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(type), pt_ce_union_type)) continue;
344+
/* a member that is not an object is rejected here: the twin
345+
* stores it unchecked and fails at its first use, but the
346+
* native code reading the members — in this family and in
347+
* every port iterating getTypes() — relies on objects */
348+
if (UNEXPECTED(Z_TYPE_P(type) != IS_OBJECT)) {
349+
zend_type_error("phpstan_turbo: %s::__construct(): every member must be a %s, %s given", ZSTR_VAL(pt_ce_union_type->name), ptcls::type, zend_zval_value_name(type));
350+
return false;
351+
}
352+
if (!instanceof_function(Z_OBJCE_P(type), pt_ce_union_type)) continue;
345353
bool isTemplate;
346354
if (UNEXPECTED(!isInstance(type, PT_CLASS_TEMPLATE_TYPE, isTemplate))) return false;
347355
if (isTemplate) continue;

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8330,6 +8330,47 @@ public function getResolvedPhpDoc(): ?\PHPStan\PhpDoc\ResolvedPhpDocBlock { retu
83308330
$r["objectShape traverse returning $valueName describe"] = $misuse(static fn () => $misuseGoodShape->traverse(static fn () => $value)->describe(\PHPStan\Type\VerbosityLevel::precise()), $withMessage);
83318331
}
83328332

8333+
// compound types with a member that is not an object: the twin stores it
8334+
// and fails at its first use, the native rejects it at construction —
8335+
// observed through operations the twin fails with a TypeError, and as
8336+
// "some Error" through those the twin fails with a method call on it
8337+
foreach ([\PHPStan\Type\IntersectionType::class, \PHPStan\Type\UnionType::class, \PHPStan\Type\BenevolentUnionType::class] as $compoundClass) {
8338+
foreach (['first' => static fn () => [5, new \PHPStan\Type\IntegerType()], 'second' => static fn () => [new \PHPStan\Type\IntegerType(), 5]] as $position => $members) {
8339+
foreach ([
8340+
'describe' => static fn (\PHPStan\Type\Type $t) => $t->describe(\PHPStan\Type\VerbosityLevel::precise()),
8341+
'isSuperTypeOf' => static fn (\PHPStan\Type\Type $t) => $t->isSuperTypeOf(new \PHPStan\Type\IntegerType()),
8342+
'isString' => static fn (\PHPStan\Type\Type $t) => $t->isString(),
8343+
'getIterableValueType' => static fn (\PHPStan\Type\Type $t) => $t->getIterableValueType(),
8344+
'toPhpDocNode' => static fn (\PHPStan\Type\Type $t) => $t->toPhpDocNode(),
8345+
] as $method => $call) {
8346+
$r["compound $compoundClass $position $method"] = $misuse(static fn () => $call(new $compoundClass($members())));
8347+
}
8348+
foreach ([
8349+
'hasTemplateOrLateResolvableType' => static fn (\PHPStan\Type\Type $t) => $t->hasTemplateOrLateResolvableType(),
8350+
'getOffsetValueType' => static fn (\PHPStan\Type\Type $t) => $t->getOffsetValueType(new \PHPStan\Type\IntegerType()),
8351+
'union' => static fn (\PHPStan\Type\Type $t) => \PHPStan\Type\TypeCombinator::union($t, new \PHPStan\Type\StringType()),
8352+
] as $method => $call) {
8353+
try {
8354+
$call(new $compoundClass($members()));
8355+
$r["compound $compoundClass $position $method"] = 'no error';
8356+
} catch (\Error $e) {
8357+
$r["compound $compoundClass $position $method"] = 'Error';
8358+
}
8359+
}
8360+
}
8361+
}
8362+
// the benevolent union's getOffsetValueType() iterates $this->getTypes(),
8363+
// which a subclass may override
8364+
$misuseBenevolent = new class ([new \PHPStan\Type\IntegerType(), new \PHPStan\Type\StringType()]) extends \PHPStan\Type\BenevolentUnionType {
8365+
8366+
public function getTypes(): array
8367+
{
8368+
return [new \PHPStan\Type\IntegerType(), 5];
8369+
}
8370+
8371+
};
8372+
$r['benevolent overridden getTypes getOffsetValueType'] = $misuse(static fn () => $misuseBenevolent->getOffsetValueType(new \PHPStan\Type\IntegerType()), true);
8373+
83338374
foreach ($r as $key => $value) {
83348375
$observations["misuse $key"] = $value;
83358376
}

0 commit comments

Comments
 (0)