Property handle unset name on ReflectionClassConstant

While the typed property ensures that the value is a string,
we should make sure that we handle an unset property gracefully.

Do this by throwing the same error we would normally throw if
you access an uninitializde typed property.
This commit is contained in:
Nikita Popov 2021-05-07 12:37:50 +02:00
parent 26860c6b6a
commit 50b4a7adf9
3 changed files with 25 additions and 17 deletions

View file

@ -192,16 +192,6 @@ static inline bool is_closure_invoke(zend_class_entry *ce, zend_string *lcname)
&& zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME);
}
static void _default_get_name(zval *object, zval *return_value) /* {{{ */
{
zval *name = reflection_prop_name(object);
if (Z_ISUNDEF_P(name)) {
RETURN_FALSE;
}
ZVAL_COPY(return_value, name);
}
/* }}} */
static zend_function *_copy_function(zend_function *fptr) /* {{{ */
{
if (fptr
@ -3634,15 +3624,24 @@ ZEND_METHOD(ReflectionClassConstant, __toString)
reflection_object *intern;
zend_class_constant *ref;
smart_str str = {0};
zval name;
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
GET_REFLECTION_OBJECT_PTR(ref);
_default_get_name(ZEND_THIS, &name);
_class_const_string(&str, Z_STRVAL(name), ref, "");
zval_ptr_dtor(&name);
zval *name = reflection_prop_name(ZEND_THIS);
if (Z_ISUNDEF_P(name)) {
zend_throw_error(NULL,
"Typed property ReflectionClassConstant::$name "
"must not be accessed before initialization");
RETURN_THROWS();
}
ZVAL_DEREF(name);
ZEND_ASSERT(Z_TYPE_P(name) == IS_STRING);
_class_const_string(&str, Z_STRVAL_P(name), ref, "");
RETURN_STR(smart_str_extract(&str));
}
/* }}} */
@ -3653,7 +3652,16 @@ ZEND_METHOD(ReflectionClassConstant, getName)
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
_default_get_name(ZEND_THIS, return_value);
zval *name = reflection_prop_name(ZEND_THIS);
if (Z_ISUNDEF_P(name)) {
zend_throw_error(NULL,
"Typed property ReflectionClassConstant::$name "
"must not be accessed before initialization");
RETURN_THROWS();
}
ZVAL_COPY_DEREF(return_value, name);
}
/* }}} */

View file

@ -455,7 +455,7 @@ class ReflectionClassConstant implements Reflector
public function __toString(): string {}
/** @return string|false */
/** @return string */
public function getName() {}
/** @return mixed */

View file

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 388312e928b54992da6b7e0e0f15dec72d9290f1 */
* Stub hash: 47ac64b027cdeb0e9996147277f79fa9d6b876bd */
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0)