Skip to content

Commit 6fb5349

Browse files
ondrejmirtesclaude
andcommitted
Build ClassReflection's member keys and messages binary safe
The native ClassReflection built the scope-qualified member cache key ("%s-%s"), the display name with template types and the missing enum case message with zend_strpprintf(), whose %s stops at a NUL byte. A member name with a NUL byte in a class scope then shared the cache key of its prefix (getMethod("area\0x") answered area()), and the message lost the rest of the name. Build them length-aware, like the twin's sprintf() and concatenation. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent c1a8fd5 commit 6fb5349

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

‎turbo-ext/src/ClassReflection.cpp‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,13 @@ class ClassReflection
11421142
zv::Val name = displayName();
11431143
if (UNEXPECTED(name.isUndef())) return zv::Val();
11441144
zv::Val joined = implodeComma(templateTypes.ref());
1145-
return zv::Val::adoptString(zend_strpprintf(0, "%s<%s>", Z_STRVAL_P(name.raw()), Z_STRVAL_P(joined.raw())));
1145+
/* binary safe, like the twin's concatenation */
1146+
smart_str displayName = {};
1147+
smart_str_appendl(&displayName, Z_STRVAL_P(name.raw()), Z_STRLEN_P(name.raw()));
1148+
smart_str_appendc(&displayName, '<');
1149+
smart_str_appendl(&displayName, Z_STRVAL_P(joined.raw()), Z_STRLEN_P(joined.raw()));
1150+
smart_str_appendc(&displayName, '>');
1151+
return zv::Val::adoptString(smart_str_extract(&displayName));
11461152
}
11471153

11481154
zv::Val getCacheKey()
@@ -1604,7 +1610,8 @@ class ClassReflection
16041610
zv::Val cacheKey = crGetCacheKey(classReflection.ref());
16051611
if (UNEXPECTED(cacheKey.isUndef())) return zv::Val();
16061612
zend_string *cacheKeyStr = zval_get_string(cacheKey.raw());
1607-
zv::Val key = zv::Val::adoptString(zend_strpprintf(0, "%s-%s", ZSTR_VAL(memberName), ZSTR_VAL(cacheKeyStr)));
1613+
/* sprintf('%s-%s', ...) is binary safe */
1614+
zv::Val key = zv::Val::adoptString(zend_string_concat3(ZSTR_VAL(memberName), ZSTR_LEN(memberName), "-", 1, ZSTR_VAL(cacheKeyStr), ZSTR_LEN(cacheKeyStr)));
16081615
zend_string_release(cacheKeyStr);
16091616
return key;
16101617
}
@@ -2191,7 +2198,14 @@ class ClassReflection
21912198
if (UNEXPECTED(displayName_.isUndef())) return zv::Val();
21922199
zend_string *displayNameStr = zval_get_string(displayName_.raw());
21932200
zval message;
2194-
ZVAL_STR(&message, zend_strpprintf(0, "Enum case %s::%s does not exist.", ZSTR_VAL(displayNameStr), ZSTR_VAL(name)));
2201+
/* binary safe, like the twin's sprintf() */
2202+
smart_str text = {};
2203+
smart_str_appends(&text, "Enum case ");
2204+
smart_str_append(&text, displayNameStr);
2205+
smart_str_appends(&text, "::");
2206+
smart_str_append(&text, name);
2207+
smart_str_appends(&text, " does not exist.");
2208+
ZVAL_STR(&message, smart_str_extract(&text));
21952209
zend_string_release(displayNameStr);
21962210
throwNew(PT_CLASS_SHOULD_NOT_HAPPEN, 1, &message);
21972211
zval_ptr_dtor(&message);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,11 +577,11 @@ public function norm(mixed $value): mixed
577577
$rfSamples['MixinHolder<int>'] = $rfSamples['ReflectionFamilyFixture\MixinHolder']->withTypes([new \PHPStan\Type\IntegerType()]);
578578
$rfSamples['ImmutableChild final'] = $rfSamples['ReflectionFamilyFixture\ImmutableChild']->asFinal();
579579

580-
$rfProbeMethods = ['__construct', '__get', '__set', '__isset', 'area', 'greet', 'secret', 'make', 'base', 'hidden', 'label', 'getIterator', 'count', 'name', 'find', 'extra', 'cases', 'from', 'tryFrom', 'offsetGet', 'getMessage', 'nope', '123', 'Legacy'];
581-
$rfProbeProperties = ['x', 'pub', 'priv', 'count', 'fromTrait', 'a', 's', 'p', 'c', 'v', 'value', 'name', 'nope', 'message', '123'];
580+
$rfProbeMethods = ['__construct', '__get', '__set', '__isset', 'area', 'greet', 'secret', 'make', 'base', 'hidden', 'label', 'getIterator', 'count', 'name', 'find', 'extra', 'cases', 'from', 'tryFrom', 'offsetGet', 'getMessage', 'nope', '123', 'Legacy', "area\0x"];
581+
$rfProbeProperties = ['x', 'pub', 'priv', 'count', 'fromTrait', 'a', 's', 'p', 'c', 'v', 'value', 'name', 'nope', 'message', '123', "pub\0x"];
582582
$rfProbeTraits = ['ReflectionFamilyFixture\Greets', 'ReflectionFamilyFixture\Nested', 'ReflectionFamilyFixture\Documented', 'ReflectionFamilyFixture\Twice', 'Nope\Missing'];
583583
$rfProbeConstants = ['DEFAULT', 'LIMIT', 'NAME', 'INHERITED', 'DOCUMENTED', 'TEMPLATED', 'OLD', 'TYPED', 'FIRST', 'nope', 'TARGET_CLASS'];
584-
$rfProbeEnumCases = ['Hearts', 'Spades', 'A', 'B', 'Nope'];
584+
$rfProbeEnumCases = ['Hearts', 'Spades', 'A', 'B', 'Nope', "Hearts\0x"];
585585
$rfProbeClasses = ['ReflectionFamilyFixture\Base', 'ReflectionFamilyFixture\Shape', 'ReflectionFamilyFixture\Circle', 'ReflectionFamilyFixture\Box', 'ReflectionFamilyFixture\HasName', 'ReflectionFamilyFixture\Dynamic', 'Countable', 'Traversable', 'IteratorAggregate', 'stdClass', 'Throwable', 'Nope\Missing'];
586586
// the memo slots the ported methods own (a slot an unported method fills
587587
// lands in the delegate twin on the native side) and the constructor's

0 commit comments

Comments
 (0)