Skip to content

Commit b3ac0a4

Browse files
ondrejmirtesclaude
andcommitted
Do not addref interned parameter names in native PhpDocsResolver
positionalNames() collected each parameter's Variable name with Z_ADDREF_P, which increments the refcount of an interned string too. Names built by PHP code (a `new Variable('foo')` literal) are interned, and with opcache those live in shared memory: the write faults under opcache.protect_memory=1 and trips the refcount assertion of debug PHP builds. Z_TRY_ADDREF_P leaves non-refcounted strings alone. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EKX5yrrUnnbeRtc2oURC2u
1 parent 40c6861 commit b3ac0a4

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PhpParser\Comment\Doc;
6+
use PhpParser\Node\Expr\Variable;
7+
use PhpParser\Node\Identifier;
8+
use PhpParser\Node\Param;
9+
use PhpParser\Node\Stmt\ClassMethod;
10+
use PHPStan\Testing\PHPStanTestCase;
11+
use PHPStan\Type\VerbosityLevel;
12+
use function array_map;
13+
14+
class PhpDocsResolverTest extends PHPStanTestCase
15+
{
16+
17+
public function testParameterNamesFromPhpBuiltNodes(): void
18+
{
19+
$file = __DIR__ . '/data/php-docs-resolver-parameter-names.php';
20+
require_once $file;
21+
22+
$reflectionProvider = self::createReflectionProvider();
23+
$scope = self::createScopeFactory($reflectionProvider, self::getContainer()->getService('typeSpecifier'))
24+
->create(ScopeContext::create($file))
25+
->enterClass($reflectionProvider->getClass('PhpDocsResolverParameterNames\Foo'));
26+
27+
// the names are PHP literals (interned strings), unlike the ones
28+
// the parser allocates
29+
$node = new ClassMethod(new Identifier('doFoo'), [
30+
'params' => [
31+
new Param(new Variable('a')),
32+
new Param(new Variable('count')),
33+
],
34+
], [
35+
'comments' => [
36+
new Doc("/**\n\t * @param non-empty-string \$a\n\t * @param positive-int \$count\n\t */"),
37+
],
38+
]);
39+
40+
$phpDocParameterTypes = self::getContainer()->getByType(PhpDocsResolver::class)->getPhpDocs($scope, $node)[1];
41+
42+
$this->assertSame([
43+
'a' => 'non-empty-string',
44+
'count' => 'int<1, max>',
45+
], array_map(static fn ($type) => $type->describe(VerbosityLevel::precise()), $phpDocParameterTypes));
46+
}
47+
48+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace PhpDocsResolverParameterNames;
4+
5+
class Foo
6+
{
7+
8+
/**
9+
* @param non-empty-string $a
10+
* @param positive-int $count
11+
*/
12+
public function doFoo($a, $count): void
13+
{
14+
}
15+
16+
}

‎turbo-ext/src/PhpDocsResolver.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ class PhpDocsResolver
640640
return zv::Val();
641641
}
642642
names.separate();
643-
Z_ADDREF_P(name);
643+
Z_TRY_ADDREF_P(name);
644644
zend_string *key = entry.stringKeyOrNull();
645645
if (key != NULL) {
646646
zend_hash_update(names.table(), key, name);

0 commit comments

Comments
 (0)