Skip to content

Commit c8abc66

Browse files
committed
Fix lookup of lc namespace
1 parent a707f87 commit c8abc66

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

ext/reflection/php_reflection.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7210,18 +7210,33 @@ static zval *_reflection_write_property(zend_object *object, zend_string *name,
72107210

72117211
ZEND_METHOD(ReflectionConstant, __construct)
72127212
{
7213-
zend_string *const_name;
7213+
zend_string *name;
72147214

72157215
zval *object = ZEND_THIS;
72167216
reflection_object *intern = Z_REFLECTION_P(object);
72177217

72187218
ZEND_PARSE_PARAMETERS_START(1, 1)
7219-
Z_PARAM_STR(const_name)
7219+
Z_PARAM_STR(name)
72207220
ZEND_PARSE_PARAMETERS_END();
72217221

7222-
zval *const_zv = zend_get_constant(const_name);
7222+
/* Build name with lowercased ns. */
7223+
bool backslash_prefixed = ZSTR_VAL(name)[0] == '\\';
7224+
char *source = ZSTR_VAL(name) + backslash_prefixed;
7225+
size_t source_len = ZSTR_LEN(name) - backslash_prefixed;
7226+
zend_string *lc_name = zend_string_alloc(source_len, /* persistent */ false);
7227+
const char *ns_end = zend_memrchr(source, '\\', source_len);
7228+
size_t ns_len = 0;
7229+
if (ns_end) {
7230+
ns_len = ns_end - ZSTR_VAL(name);
7231+
zend_str_tolower_copy(ZSTR_VAL(lc_name), source, ns_len);
7232+
}
7233+
memcpy(ZSTR_VAL(lc_name) + ns_len, source + ns_len, source_len - ns_len);
7234+
7235+
zval *const_zv = zend_get_constant(lc_name);
7236+
zend_string_release_ex(lc_name, /* persistent */ false);
7237+
72237238
if (!const_zv) {
7224-
zend_throw_exception_ex(reflection_exception_ptr, 0, "Constant \"%s\" does not exist", ZSTR_VAL(const_name));
7239+
zend_throw_exception_ex(reflection_exception_ptr, 0, "Constant \"%s\" does not exist", ZSTR_VAL(name));
72257240
RETURN_THROWS();
72267241
}
72277242

@@ -7230,7 +7245,7 @@ ZEND_METHOD(ReflectionConstant, __construct)
72307245
intern->ptr = const_;
72317246
intern->ref_type = REF_TYPE_OTHER;
72327247

7233-
ZVAL_STR_COPY(reflection_prop_name(object), const_name);
7248+
ZVAL_STR_COPY(reflection_prop_name(object), name);
72347249
}
72357250

72367251
ZEND_METHOD(ReflectionConstant, getName)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
ReflectionConstant with namespace
3+
--FILE--
4+
<?php
5+
6+
namespace Foo;
7+
8+
const C = 42;
9+
10+
var_dump(new \ReflectionConstant('Foo\\C'));
11+
var_dump(new \ReflectionConstant('\\Foo\\C'));
12+
13+
?>
14+
--EXPECT--
15+
object(ReflectionConstant)#1 (1) {
16+
["name"]=>
17+
string(5) "Foo\C"
18+
}
19+
object(ReflectionConstant)#1 (1) {
20+
["name"]=>
21+
string(6) "\Foo\C"
22+
}

0 commit comments

Comments
 (0)