Skip to content

Commit b622768

Browse files
committed
do visibility check during instantiation
1 parent 8f3cd23 commit b622768

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Zend/zend_API.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,30 @@ static zend_always_inline zend_result _object_and_properties_init(zval *arg, zen
18161816
return FAILURE;
18171817
}
18181818

1819+
if (class_type->required_scope) {
1820+
const zend_class_entry *scope = zend_get_executed_scope();
1821+
if (UNEXPECTED(scope == NULL)) {
1822+
zend_throw_error(NULL, "Cannot instantiate class %s from the global scope", ZSTR_VAL(class_type->name));
1823+
ZVAL_NULL(arg);
1824+
Z_OBJ_P(arg) = NULL;
1825+
return FAILURE;
1826+
}
1827+
1828+
if (class_type->required_scope_absolute) {
1829+
if (scope != class_type->required_scope && scope->lexical_scope != class_type->required_scope) {
1830+
zend_throw_error(NULL, "Cannot instantiate private class %s from %s", ZSTR_VAL(class_type->name), ZSTR_VAL(scope->name));
1831+
ZVAL_NULL(arg);
1832+
Z_OBJ_P(arg) = NULL;
1833+
return FAILURE;
1834+
}
1835+
} else if (!instanceof_function(scope, class_type->required_scope) && !instanceof_function(scope->lexical_scope, class_type->required_scope)) {
1836+
zend_throw_error(NULL, "Cannot instantiate protected class %s from %s", ZSTR_VAL(class_type->name), ZSTR_VAL(scope->name));
1837+
ZVAL_NULL(arg);
1838+
Z_OBJ_P(arg) = NULL;
1839+
return FAILURE;
1840+
}
1841+
}
1842+
18191843
if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
18201844
if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
18211845
ZVAL_NULL(arg);

0 commit comments

Comments
 (0)