Skip to content

Commit 8c2f7b5

Browse files
ondrejmirtesclaude
andcommitted
Check the traverse() callback result of ObjectWithoutClassType and StaticType
The native traverse() handed whatever the callback returned to the constructor, which reads the class entry of the value, so a callback returning a string or an int crashed the process. The twins pass the result into a typed `?Type $subtractedType` parameter and throw a TypeError instead; the natives now raise the same TypeError for any value that is neither null nor a Type. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 2c6c726 commit 8c2f7b5

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎turbo-ext/src/ObjectWithoutClassType.cpp‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,15 @@ class ObjectWithoutClassType
251251
if (UNEXPECTED(!pt_call_fci(fci, fcc, 1, subtracted, &mapped))) return zv::Val();
252252
zv::Val mappedType = zv::Val::adopt(mapped);
253253
if (Z_TYPE(mapped) == IS_OBJECT && Z_OBJ(mapped) == Z_OBJ_P(subtracted)) return thisValue();
254-
return create(Z_TYPE(mapped) == IS_NULL ? NULL : mappedType.raw());
254+
if (Z_TYPE(mapped) == IS_NULL) return create();
255+
/* the twin's `new self($subtractedType)` checks its `?Type` parameter */
256+
bool isType;
257+
if (UNEXPECTED(!pt_type_instanceof(mappedType.raw(), PT_CLASS_TYPE, isType))) return zv::Val();
258+
if (UNEXPECTED(!isType)) {
259+
zend_type_error("%s::__construct(): Argument #1 ($subtractedType) must be of type ?%s, %s given", ZSTR_VAL(pt_ce_object_without_class_type->name), ptcls::type, zend_zval_value_name(mappedType.raw()));
260+
return zv::Val();
261+
}
262+
return create(mappedType.raw());
255263
}
256264

257265
/* $this without a subtracted type, new self() with one; UNDEF = pending

‎turbo-ext/src/StaticType.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,16 @@ class StaticType
552552
if (Z_TYPE(mapped) == IS_OBJECT && Z_OBJ(mapped) == Z_OBJ_P(subtracted)) return thisValue();
553553
zval *reflection = classReflection();
554554
if (UNEXPECTED(reflection == NULL)) return zv::Val();
555-
return create(reflection, Z_TYPE(mapped) == IS_NULL ? NULL : mappedType.raw());
555+
if (Z_TYPE(mapped) == IS_NULL) return create(reflection);
556+
/* the twin's `new self($this->classReflection, $subtractedType)`
557+
* checks its `?Type` parameter */
558+
bool isType;
559+
if (UNEXPECTED(!pt_type_instanceof(mappedType.raw(), PT_CLASS_TYPE, isType))) return zv::Val();
560+
if (UNEXPECTED(!isType)) {
561+
zend_type_error("%s::__construct(): Argument #2 ($subtractedType) must be of type ?%s, %s given", ZSTR_VAL(pt_ce_static_type->name), ptcls::type, zend_zval_value_name(mappedType.raw()));
562+
return zv::Val();
563+
}
564+
return create(reflection, mappedType.raw());
556565
}
557566

558567
/* $this without a subtracted type, new self($this->classReflection)

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8276,6 +8276,32 @@ public function getResolvedPhpDoc(): ?\PHPStan\PhpDoc\ResolvedPhpDocBlock { retu
82768276
}
82778277
}
82788278

8279+
// ---- lane types: misuse parity ----
8280+
// misuse the twins reject with an exception must raise the same exception
8281+
// natively (never crash): a result is observed by its view, an exception by
8282+
// its class, and by its message where the message does not name a file
8283+
{
8284+
$misuse = static function (callable $fn, bool $withMessage = false) use ($view): mixed {
8285+
try {
8286+
return ['ok', $view($fn())];
8287+
} catch (\Throwable $e) {
8288+
return $withMessage ? [get_class($e), $e->getMessage()] : [get_class($e)];
8289+
}
8290+
};
8291+
$r = [];
8292+
$misuseStaticReflection = $stringReflectionProvider->getClass(\PHPStan\TrinaryLogic::class);
8293+
8294+
// traverse() callbacks returning a non-Type into a typed ?Type parameter
8295+
foreach (['string' => 'not-a-type', 'int' => 1, 'object' => new \stdClass(), 'null' => null] as $returnedName => $returned) {
8296+
$r["traverse objectWithoutClass returning $returnedName"] = $misuse(static fn () => (new \PHPStan\Type\ObjectWithoutClassType(new \PHPStan\Type\StringType()))->traverse(static fn () => $returned));
8297+
$r["traverse static returning $returnedName"] = $misuse(static fn () => (new \PHPStan\Type\StaticType($misuseStaticReflection, new \PHPStan\Type\StringType()))->traverse(static fn () => $returned));
8298+
}
8299+
8300+
foreach ($r as $key => $value) {
8301+
$observations["misuse $key"] = $value;
8302+
}
8303+
}
8304+
82798305
// observations holding bytes that are not UTF-8 (the invalid-UTF-8 subject's
82808306
// descriptions) go out base64-encoded so json_encode() keeps every byte; the
82818307
// non-finite floats (the NAN and infinity subjects' values) as their names

0 commit comments

Comments
 (0)