Skip to content
Merged
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
31 changes: 15 additions & 16 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -4600,19 +4600,17 @@ ZEND_METHOD(ReflectionClass, hasProperty)
}

GET_REFLECTION_OBJECT_PTR(ce);
if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
if ((property_info->flags & ZEND_ACC_PRIVATE) && property_info->ce != ce) {
RETURN_FALSE;
}
if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL
&& (!(property_info->flags & ZEND_ACC_PRIVATE)
|| property_info->ce == ce)) {
RETURN_TRUE;
} else {
if (Z_TYPE(intern->obj) != IS_UNDEF) {
if (Z_OBJ_HANDLER(intern->obj, has_property)(Z_OBJ(intern->obj), name, ZEND_PROPERTY_EXISTS, NULL)) {
RETURN_TRUE;
}
}
if (Z_TYPE(intern->obj) != IS_UNDEF) {
if (Z_OBJ_HANDLER(intern->obj, has_property)(Z_OBJ(intern->obj), name, ZEND_PROPERTY_EXISTS, NULL)) {
RETURN_TRUE;
}
RETURN_FALSE;
}
RETURN_FALSE;
}
/* }}} */

Expand All @@ -4631,12 +4629,13 @@ ZEND_METHOD(ReflectionClass, getProperty)
}

GET_REFLECTION_OBJECT_PTR(ce);
if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
if (!(property_info->flags & ZEND_ACC_PRIVATE) || property_info->ce == ce) {
reflection_property_factory(ce, name, property_info, return_value);
return;
}
} else if (Z_TYPE(intern->obj) != IS_UNDEF) {
if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL
&& (!(property_info->flags & ZEND_ACC_PRIVATE)
|| property_info->ce == ce)) {
reflection_property_factory(ce, name, property_info, return_value);
return;
}
if (Z_TYPE(intern->obj) != IS_UNDEF) {
/* Check for dynamic properties */
if (zend_hash_exists(Z_OBJ_HT(intern->obj)->get_properties(Z_OBJ(intern->obj)), name)) {
reflection_property_factory(ce, name, NULL, return_value);
Expand Down
44 changes: 44 additions & 0 deletions ext/reflection/tests/gh22441.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
GH-22441 (ReflectionClass::hasProperty()/getProperty() ignore dynamic properties shadowing a private parent property)
--FILE--
<?php

class Base {
private mixed $shadow;
private mixed $onlyBase;
}

#[AllowDynamicProperties]
class Child extends Base {}

$o = new Child();
$o->shadow = true;
$o->noShadow = true;

$r = new ReflectionObject($o);

echo "hasProperty:\n";
echo "shadow (dynamic over private parent): "; var_dump($r->hasProperty('shadow'));
echo "noShadow (plain dynamic): "; var_dump($r->hasProperty('noShadow'));
echo "onlyBase (private parent, no dynamic): "; var_dump($r->hasProperty('onlyBase'));

echo "\ngetProperty:\n";
foreach (['shadow', 'noShadow', 'onlyBase'] as $name) {
try {
$p = $r->getProperty($name);
printf("%s: %s::\$%s\n", $name, $p->getDeclaringClass()->getName(), $p->getName());
} catch (ReflectionException $e) {
printf("%s: %s\n", $name, $e->getMessage());
}
}
?>
--EXPECT--
hasProperty:
shadow (dynamic over private parent): bool(true)
noShadow (plain dynamic): bool(true)
onlyBase (private parent, no dynamic): bool(false)

getProperty:
shadow: Child::$shadow
noShadow: Child::$noShadow
onlyBase: Property Child::$onlyBase does not exist
4 changes: 4 additions & 0 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,8 @@ PHP_METHOD(SplFileObject, next)

ZEND_PARSE_PARAMETERS_NONE();

CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);

if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval)) {
if (spl_filesystem_file_read_line(ZEND_THIS, intern, true) == FAILURE) {
return;
Expand Down Expand Up @@ -2300,6 +2302,8 @@ PHP_METHOD(SplFileObject, fputcsv)
RETURN_THROWS();
}

CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);

if (delim) {
if (d_len != 1) {
zend_argument_value_error(2, "must be a single character");
Expand Down
35 changes: 35 additions & 0 deletions ext/spl/tests/gh16217.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
GH-16217 (SplFileObject methods on an uninitialized object segfault)
--FILE--
<?php
function uninitialized(): SplFileObject {
return (new ReflectionClass(SplFileObject::class))->newInstanceWithoutConstructor();
}

try {
(new ReflectionMethod(SplFileObject::class, "fputcsv"))->invoke(uninitialized(), []);
} catch (Error $e) {
echo "fputcsv: ", $e->getMessage(), "\n";
}

try {
(new ReflectionMethod(SplFileObject::class, "next"))->invoke(uninitialized());
} catch (Error $e) {
echo "next: ", $e->getMessage(), "\n";
}

$obj = uninitialized();
(new ReflectionMethod(SplFileObject::class, "setFlags"))->invoke($obj, SplFileObject::READ_AHEAD);
try {
(new ReflectionMethod(SplFileObject::class, "next"))->invoke($obj);
} catch (Error $e) {
echo "next (READ_AHEAD): ", $e->getMessage(), "\n";
}

echo "Done\n";
?>
--EXPECT--
fputcsv: Object not initialized
next: Object not initialized
next (READ_AHEAD): Object not initialized
Done