Skip to content

Cleanup argument handling in ext/reflection #5850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *object) /* {
}
/* }}} */

ZEND_API const zend_function *zend_get_closure_method_def(zval *obj) /* {{{ */
ZEND_API const zend_function *zend_get_closure_method_def(zend_object *obj) /* {{{ */
{
zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
zend_closure *closure = (zend_closure *) obj;
return &closure->func;
}
/* }}} */
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_closures.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extern ZEND_API zend_class_entry *zend_ce_closure;
ZEND_API void zend_create_closure(zval *res, zend_function *op_array, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr);
ZEND_API void zend_create_fake_closure(zval *res, zend_function *op_array, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr);
ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *obj);
ZEND_API const zend_function *zend_get_closure_method_def(zval *obj);
ZEND_API const zend_function *zend_get_closure_method_def(zend_object *obj);
ZEND_API zval* zend_get_closure_this_ptr(zval *obj);

END_EXTERN_C()
Expand Down
199 changes: 83 additions & 116 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1480,25 +1480,24 @@ ZEND_METHOD(Reflection, getModifierNames)
ZEND_METHOD(ReflectionFunction, __construct)
{
zval *object;
zval *closure = NULL;
zend_object *closure_obj = NULL;
reflection_object *intern;
zend_function *fptr;
zend_string *fname, *lcname;

object = ZEND_THIS;
intern = Z_REFLECTION_P(object);

if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "O", &closure, zend_ce_closure) == SUCCESS) {
fptr = (zend_function*)zend_get_closure_method_def(closure);
} else {
ALLOCA_FLAG(use_heap)

if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &fname) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR_OR_OBJ_OF_CLASS(fname, closure_obj, zend_ce_closure)
ZEND_PARSE_PARAMETERS_END();

if (closure_obj) {
fptr = (zend_function*)zend_get_closure_method_def(closure_obj);
} else {
if (UNEXPECTED(ZSTR_VAL(fname)[0] == '\\')) {
/* Ignore leading "\" */
ALLOCA_FLAG(use_heap)
ZSTR_ALLOCA_ALLOC(lcname, ZSTR_LEN(fname) - 1, use_heap);
zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(fname) + 1, ZSTR_LEN(fname) - 1);
fptr = zend_fetch_function(lcname);
Expand All @@ -1519,8 +1518,8 @@ ZEND_METHOD(ReflectionFunction, __construct)
ZVAL_STR_COPY(reflection_prop_name(object), fptr->common.function_name);
intern->ptr = fptr;
intern->ref_type = REF_TYPE_FUNCTION;
if (closure) {
ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(closure));
if (closure_obj) {
ZVAL_OBJ_COPY(&intern->obj, closure_obj);
} else {
ZVAL_UNDEF(&intern->obj);
}
Expand Down Expand Up @@ -1603,7 +1602,7 @@ ZEND_METHOD(ReflectionFunctionAbstract, getClosureScopeClass)
}
GET_REFLECTION_OBJECT();
if (!Z_ISUNDEF(intern->obj)) {
closure_func = zend_get_closure_method_def(&intern->obj);
closure_func = zend_get_closure_method_def(Z_OBJ(intern->obj));
if (closure_func && closure_func->common.scope) {
zend_reflection_class_factory(closure_func->common.scope, return_value);
}
Expand Down Expand Up @@ -2315,7 +2314,7 @@ ZEND_METHOD(ReflectionParameter, __construct)
ce = Z_OBJCE_P(reference);

if (instanceof_function(ce, zend_ce_closure)) {
fptr = (zend_function *)zend_get_closure_method_def(reference);
fptr = (zend_function *)zend_get_closure_method_def(Z_OBJ_P(reference));
Z_ADDREF_P(reference);
is_closure = 1;
} else if ((fptr = zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) == NULL) {
Expand Down Expand Up @@ -3017,7 +3016,6 @@ ZEND_METHOD(ReflectionMethod, __construct)
object = ZEND_THIS;
intern = Z_REFLECTION_P(object);

/* Find the class entry */
switch (Z_TYPE_P(classname)) {
case IS_STRING:
if ((ce = zend_lookup_class(Z_STR_P(classname))) == NULL) {
Expand Down Expand Up @@ -3519,38 +3517,31 @@ ZEND_METHOD(ReflectionMethod, setAccessible)
/* {{{ Constructor. Throws an Exception in case the given class constant does not exist */
ZEND_METHOD(ReflectionClassConstant, __construct)
{
zval *classname, *object;
zval *object;
zend_string *classname_str;
zend_object *classname_obj;
zend_string *constname;
reflection_object *intern;
zend_class_entry *ce;
zend_class_constant *constant = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS", &classname, &constname) == FAILURE) {
RETURN_THROWS();
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR_OR_OBJ(classname_str, classname_obj)
Z_PARAM_STR(constname)
ZEND_PARSE_PARAMETERS_END();

if (classname_obj) {
ce = classname_obj->ce;
} else {
if ((ce = zend_lookup_class(classname_str)) == NULL) {
zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(classname_str));
RETURN_THROWS();
}
}

object = ZEND_THIS;
intern = Z_REFLECTION_P(object);

/* Find the class entry */
switch (Z_TYPE_P(classname)) {
case IS_STRING:
if ((ce = zend_lookup_class(Z_STR_P(classname))) == NULL) {
zend_throw_exception_ex(reflection_exception_ptr, 0,
"Class \"%s\" does not exist", Z_STRVAL_P(classname));
RETURN_THROWS();
}
break;

case IS_OBJECT:
ce = Z_OBJCE_P(classname);
break;

default:
zend_argument_error(reflection_exception_ptr, 1, "must be of type object|string, %s given", zend_zval_type_name(classname));
RETURN_THROWS();
}

if ((constant = zend_hash_find_ptr(&ce->constants_table, constname)) == NULL) {
zend_throw_exception_ex(reflection_exception_ptr, 0, "Constant %s::%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(constname));
RETURN_THROWS();
Expand Down Expand Up @@ -3814,7 +3805,7 @@ ZEND_METHOD(ReflectionClass, getStaticProperties)
GET_REFLECTION_OBJECT_PTR(ce);

if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
return;
RETURN_THROWS();
}

if (ce->default_static_members_count && !CE_STATIC_MEMBERS(ce)) {
Expand Down Expand Up @@ -3863,7 +3854,7 @@ ZEND_METHOD(ReflectionClass, getStaticPropertyValue)
GET_REFLECTION_OBJECT_PTR(ce);

if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
return;
RETURN_THROWS();
}

old_scope = EG(fake_scope);
Expand Down Expand Up @@ -3901,7 +3892,7 @@ ZEND_METHOD(ReflectionClass, setStaticPropertyValue)
GET_REFLECTION_OBJECT_PTR(ce);

if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
return;
RETURN_THROWS();
}
old_scope = EG(fake_scope);
EG(fake_scope) = ce;
Expand Down Expand Up @@ -3945,7 +3936,7 @@ ZEND_METHOD(ReflectionClass, getDefaultProperties)
GET_REFLECTION_OBJECT_PTR(ce);
array_init(return_value);
if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
return;
RETURN_THROWS();
}
add_class_vars(ce, 1, return_value);
add_class_vars(ce, 0, return_value);
Expand Down Expand Up @@ -4967,38 +4958,30 @@ ZEND_METHOD(ReflectionClass, isSubclassOf)
{
reflection_object *intern, *argument;
zend_class_entry *ce, *class_ce;
zval *class_name;
zend_string *class_str;
zend_object *class_obj;

GET_REFLECTION_OBJECT_PTR(ce);
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR_OR_OBJ_OF_CLASS(class_str, class_obj, reflection_class_ptr)
ZEND_PARSE_PARAMETERS_END();

if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &class_name) == FAILURE) {
RETURN_THROWS();
}
if (class_obj) {
argument = reflection_object_from_obj(class_obj);
if (argument->ptr == NULL) {
zend_throw_error(NULL, "Internal error: Failed to retrieve the argument's reflection object");
RETURN_THROWS();
}

switch (Z_TYPE_P(class_name)) {
case IS_STRING:
if ((class_ce = zend_lookup_class(Z_STR_P(class_name))) == NULL) {
zend_throw_exception_ex(reflection_exception_ptr, 0,
"Class \"%s\" does not exist", Z_STRVAL_P(class_name));
RETURN_THROWS();
}
break;
case IS_OBJECT:
if (instanceof_function(Z_OBJCE_P(class_name), reflection_class_ptr)) {
argument = Z_REFLECTION_P(class_name);
if (argument->ptr == NULL) {
zend_throw_error(NULL, "Internal error: Failed to retrieve the argument's reflection object");
RETURN_THROWS();
}
class_ce = argument->ptr;
break;
}
/* no break */
default:
zend_argument_error(reflection_exception_ptr, 1, "must be of type ReflectionClass|string, %s given", zend_zval_type_name(class_name));
class_ce = argument->ptr;
} else {
if ((class_ce = zend_lookup_class(class_str)) == NULL) {
zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(class_str));
RETURN_THROWS();
}
}

GET_REFLECTION_OBJECT_PTR(ce);

RETURN_BOOL((ce != class_ce && instanceof_function(ce, class_ce)));
}
/* }}} */
Expand All @@ -5007,44 +4990,36 @@ ZEND_METHOD(ReflectionClass, isSubclassOf)
ZEND_METHOD(ReflectionClass, implementsInterface)
{
reflection_object *intern, *argument;
zend_string *interface_str;
zend_class_entry *ce, *interface_ce;
zval *interface;
zend_object *interface_obj;

GET_REFLECTION_OBJECT_PTR(ce);
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR_OR_OBJ_OF_CLASS(interface_str, interface_obj, reflection_class_ptr)
ZEND_PARSE_PARAMETERS_END();

if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &interface) == FAILURE) {
RETURN_THROWS();
}
if (interface_obj) {
argument = reflection_object_from_obj(interface_obj);
if (argument->ptr == NULL) {
zend_throw_error(NULL, "Internal error: Failed to retrieve the argument's reflection object");
RETURN_THROWS();
}

switch (Z_TYPE_P(interface)) {
case IS_STRING:
if ((interface_ce = zend_lookup_class(Z_STR_P(interface))) == NULL) {
zend_throw_exception_ex(reflection_exception_ptr, 0,
"Interface \"%s\" does not exist", Z_STRVAL_P(interface));
RETURN_THROWS();
}
break;
case IS_OBJECT:
if (instanceof_function(Z_OBJCE_P(interface), reflection_class_ptr)) {
argument = Z_REFLECTION_P(interface);
if (argument->ptr == NULL) {
zend_throw_error(NULL, "Internal error: Failed to retrieve the argument's reflection object");
RETURN_THROWS();
}
interface_ce = argument->ptr;
break;
}
/* no break */
default:
zend_argument_error(reflection_exception_ptr, 1, "must be of type ReflectionClass|string, %s given", zend_zval_type_name(interface));
interface_ce = argument->ptr;
} else {
if ((interface_ce = zend_lookup_class(interface_str)) == NULL) {
zend_throw_exception_ex(reflection_exception_ptr, 0, "Interface \"%s\" does not exist", ZSTR_VAL(interface_str));
RETURN_THROWS();
}
}

if (!(interface_ce->ce_flags & ZEND_ACC_INTERFACE)) {
zend_throw_exception_ex(reflection_exception_ptr, 0,
"%s is not an interface", ZSTR_VAL(interface_ce->name));
zend_throw_exception_ex(reflection_exception_ptr, 0, "%s is not an interface", ZSTR_VAL(interface_ce->name));
RETURN_THROWS();
}

GET_REFLECTION_OBJECT_PTR(ce);

RETURN_BOOL(instanceof_function(ce, interface_ce));
}
/* }}} */
Expand Down Expand Up @@ -5178,7 +5153,8 @@ ZEND_METHOD(ReflectionObject, __construct)
/* {{{ Constructor. Throws an Exception in case the given property does not exist */
ZEND_METHOD(ReflectionProperty, __construct)
{
zval *classname;
zend_string *classname_str;
zend_object *classname_obj;
zend_string *name;
int dynam_prop = 0;
zval *object;
Expand All @@ -5187,39 +5163,30 @@ ZEND_METHOD(ReflectionProperty, __construct)
zend_property_info *property_info = NULL;
property_reference *reference;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS", &classname, &name) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR_OR_OBJ(classname_str, classname_obj)
Z_PARAM_STR(name)
ZEND_PARSE_PARAMETERS_END();

object = ZEND_THIS;
intern = Z_REFLECTION_P(object);

/* Find the class entry */
switch (Z_TYPE_P(classname)) {
case IS_STRING:
if ((ce = zend_lookup_class(Z_STR_P(classname))) == NULL) {
zend_throw_exception_ex(reflection_exception_ptr, 0,
"Class \"%s\" does not exist", Z_STRVAL_P(classname));
RETURN_THROWS();
}
break;

case IS_OBJECT:
ce = Z_OBJCE_P(classname);
break;

default:
zend_argument_error(reflection_exception_ptr, 1, "must be of type object|string, %s given", zend_zval_type_name(classname));
if (classname_obj) {
ce = classname_obj->ce;
} else {
if ((ce = zend_lookup_class(classname_str)) == NULL) {
zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(classname_str));
RETURN_THROWS();
}
}

property_info = zend_hash_find_ptr(&ce->properties_info, name);
if (property_info == NULL
|| ((property_info->flags & ZEND_ACC_PRIVATE)
&& property_info->ce != ce)) {
/* Check for dynamic properties */
if (property_info == NULL && Z_TYPE_P(classname) == IS_OBJECT) {
if (zend_hash_exists(Z_OBJ_HT_P(classname)->get_properties(Z_OBJ_P(classname)), name)) {
if (property_info == NULL && classname_obj) {
if (zend_hash_exists(classname_obj->handlers->get_properties(classname_obj), name)) {
dynam_prop = 1;
}
}
Expand Down
Loading