Skip to content

ext/standard/dir.c: Directory class should behave like other opaque objects #15886

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

Merged
merged 3 commits into from
Oct 27, 2024
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
13 changes: 13 additions & 0 deletions ext/standard/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ php_dir_globals dir_globals;
#endif

static zend_class_entry *dir_class_entry_ptr;
static zend_object_handlers dir_class_object_handlers;

#define Z_DIRECTORY_PATH_P(zv) OBJ_PROP_NUM(Z_OBJ_P(zv), 0)
#define Z_DIRECTORY_HANDLE_P(zv) OBJ_PROP_NUM(Z_OBJ_P(zv), 1)

static zend_function *dir_class_get_constructor(zend_object *object)
{
zend_throw_error(NULL, "Cannot directly construct Directory, use dir() instead");
return NULL;
}

#define FETCH_DIRP() \
myself = getThis(); \
if (!myself) { \
Expand Down Expand Up @@ -115,6 +122,12 @@ PHP_MINIT_FUNCTION(dir)
register_dir_symbols(module_number);

dir_class_entry_ptr = register_class_Directory();
dir_class_entry_ptr->default_object_handlers = &dir_class_object_handlers;

memcpy(&dir_class_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
dir_class_object_handlers.get_constructor = dir_class_get_constructor;
dir_class_object_handlers.clone_obj = NULL;
dir_class_object_handlers.compare = zend_objects_not_comparable;

#ifdef ZTS
ts_allocate_id(&dir_globals_id, sizeof(php_dir_globals), NULL, NULL);
Expand Down
9 changes: 5 additions & 4 deletions ext/standard/dir.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,28 @@
*/
const SCANDIR_SORT_NONE = UNKNOWN;

class Directory
/**
* @strict-properties
* @not-serializable
*/
final class Directory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the class is now final, I suppose the return types of the methods no longer need to be tentative?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually a good point, will fix this.

{
public readonly string $path;

/** @var resource */
public readonly mixed $handle;

/**
* @tentative-return-type
* @implementation-alias closedir
*/
public function close(): void {}

/**
* @tentative-return-type
* @implementation-alias rewinddir
*/
public function rewind(): void {}

/**
* @tentative-return-type
* @implementation-alias readdir
*/
public function read(): string|false {}
Expand Down
8 changes: 4 additions & 4 deletions ext/standard/dir_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions ext/standard/tests/directory/DirectoryClass_cannot_clone.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Cannot serialize instance of Directory class constructed via Reflection.
--FILE--
<?php

$d = dir(__DIR__);
try {
$cloned = clone $d;
$cloned_files = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the error is going to come from the clone call on line 8 as far as I understand, why include the rest of the try block? It shouldn't be executed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark.

while ($row = $cloned->read()){
$cloned_files[] = $row;
}
var_dump(count($cloned_files));
echo "Using original object:\n";
$original_files = [];
while ($row = $d->read()){
$original_files[] = $row;
}
var_dump(count($original_files));
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
Error: Trying to clone an uncloneable object of class Directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Cannot directly instantiate Directory class.
--FILE--
<?php

try {
$d = new Directory();
var_dump($d);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the same vein, I suggest removing the var_dump() here and in the cannot_serialize test - it should never be reached

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is to have an output in case it was able to be initialized, moreover, this was introduced in a prior commit to see the difference in behaviour.

} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
Error: Cannot directly construct Directory, use dir() instead
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Cannot serialize instance of Directory class constructed via Reflection.
--FILE--
<?php

$d = dir(__DIR__);
try {
$s = serialize($d);
var_dump($s);
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
Exception: Serialization of 'Directory' is not allowed
27 changes: 0 additions & 27 deletions ext/standard/tests/directory/DirectoryClass_error_001.phpt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Changing Directory::$handle property
--FILE--
<?php

$d = dir(__DIR__);
try {
$d->handle = "Havoc!";
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
var_dump($d->handle);

try {
unset($d->handle);
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
var_dump($d->handle);

?>
--EXPECTF--
Error: Cannot modify readonly property Directory::$handle
resource(%d) of type (stream)
Error: Cannot unset readonly property Directory::$handle
resource(%d) of type (stream)
26 changes: 26 additions & 0 deletions ext/standard/tests/directory/DirectoryClass_readonly_path.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Changing Directory::$handle property
--FILE--
<?php

$d = dir(__DIR__);
try {
$d->path = "Havoc!";
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
var_dump($d->path == __DIR__);

try {
unset($d->path);
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
var_dump($d->path == __DIR__);

?>
--EXPECTF--
Error: Cannot modify readonly property Directory::$path
bool(true)
Error: Cannot unset readonly property Directory::$path
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Cannot use instance of Directory class constructed via Reflection.
--FILE--
<?php

$rc = new ReflectionClass("Directory");
var_dump($rc->isInstantiable());
try {
$d = $rc->newInstanceWithoutConstructor();
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

var_dump($d);
try {
var_dump($d->read());
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
bool(true)
object(Directory)#2 (0) {
["path"]=>
uninitialized(string)
["handle"]=>
uninitialized(mixed)
}
Error: Unable to find my handle property
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,10 @@ echo "Structure of Directory class:\n";
$rc = new ReflectionClass("Directory");
echo $rc;

echo "Cannot instantiate a valid Directory directly:\n";
$d = new Directory(getcwd());
var_dump($d);

try {
var_dump($d->read());
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}

?>
--EXPECTF--
--EXPECT--
Structure of Directory class:
Class [ <internal%s> class Directory ] {
Class [ <internal:standard> final class Directory ] {

- Constants [0] {
}
Expand All @@ -45,29 +35,21 @@ Class [ <internal%s> class Directory ] {

- Parameters [0] {
}
- Tentative return [ void ]
- Return [ void ]
}

Method [ <internal:standard> public method rewind ] {

- Parameters [0] {
}
- Tentative return [ void ]
- Return [ void ]
}

Method [ <internal:standard> public method read ] {

- Parameters [0] {
}
- Tentative return [ string|false ]
- Return [ string|false ]
}
}
}
Cannot instantiate a valid Directory directly:
object(Directory)#%d (0) {
["path"]=>
uninitialized(string)
["handle"]=>
uninitialized(mixed)
}
Unable to find my handle property