Skip to content

Commit 841fe26

Browse files
committed
Avoid truncation on null bytes in class and function names
Follow-up to GH-22681. _class_string() and _function_string() still formatted the class name and the function name with %s, so Reflection*::__toString() cut them at the first NUL. Anonymous class names embed one, both directly and inside the name of a closure declared in such a class. Closes GH-22971
1 parent 3a9d86a commit 841fe26

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

ext/reflection/php_reflection.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, const
364364
}
365365
smart_str_append_printf(str, "class ");
366366
}
367-
smart_str_append_printf(str, "%s", ZSTR_VAL(ce->name));
367+
smart_str_append(str, ce->name);
368368
if (ce->parent) {
369369
smart_str_append_printf(str, " extends %s", ZSTR_VAL(ce->parent->name));
370370
}
@@ -904,7 +904,8 @@ static void _function_string(smart_str *str, zend_function *fptr, zend_class_ent
904904
if (fptr->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) {
905905
smart_str_appendc(str, '&');
906906
}
907-
smart_str_append_printf(str, "%s ] {\n", ZSTR_VAL(fptr->common.function_name));
907+
smart_str_append(str, fptr->common.function_name);
908+
smart_str_appends(str, " ] {\n");
908909
/* The information where a function is declared is only available for user classes */
909910
if (fptr->type == ZEND_USER_FUNCTION) {
910911
smart_str_append_printf(str, "%s @@ %s %d - %d\n", indent,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
GH-22681: null bytes in name truncate ReflectionClass::__toString()
3+
--FILE--
4+
<?php
5+
6+
$obj = new class {};
7+
8+
$r = new ReflectionClass($obj);
9+
echo $r;
10+
var_dump( $r->getName() );
11+
12+
?>
13+
--EXPECTF--
14+
Class [ <user> class class@anonymous%0%s ] {
15+
@@ %s %d-%d
16+
17+
- Constants [0] {
18+
}
19+
20+
- Static properties [0] {
21+
}
22+
23+
- Static methods [0] {
24+
}
25+
26+
- Properties [0] {
27+
}
28+
29+
- Methods [0] {
30+
}
31+
}
32+
string(%d) "class@anonymous%0%s"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
GH-22681: null bytes in name truncate ReflectionFunction::__toString()
3+
--FILE--
4+
<?php
5+
6+
$obj = new class {
7+
public function make(): Closure {
8+
return function () {};
9+
}
10+
};
11+
12+
$r = new ReflectionFunction($obj->make());
13+
echo $r;
14+
var_dump( $r->getName() );
15+
16+
?>
17+
--EXPECTF--
18+
Closure [ <user> public method {closure:class@anonymous%0%s::make():%d} ] {
19+
@@ %s %d - %d
20+
}
21+
string(%d) "{closure:class@anonymous%0%s::make():%d}"

0 commit comments

Comments
 (0)