Skip to content
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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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_classd_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_classd_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
6 changes: 5 additions & 1 deletion ext/standard/dir.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@
*/
const SCANDIR_SORT_NONE = UNKNOWN;

class Directory
/**
* @strict-properties
* @not-serializable
*/
final class Directory
{
public readonly string $path;

Expand Down
4 changes: 2 additions & 2 deletions ext/standard/dir_arginfo.h

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

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

$d = dir(__DIR__);
try {
$cloned = clone $d;
} 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,14 @@
--TEST--
Cannot directly instantiate Directory class.
--FILE--
<?php

try {
$d = new Directory();
} 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,15 @@
--TEST--
Cannot serialize instance of Directory class constructed via Reflection.
--FILE--
<?php

$d = dir(__DIR__);
try {
serialize($d);
} 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--
Structure of Directory class:
Class [ <internal%s> class Directory ] {
Class [ <internal%s> final class Directory ] {

- Constants [0] {
}
Expand Down Expand Up @@ -63,11 +53,3 @@ Class [ <internal%s> class Directory ] {
}
}
}
Cannot instantiate a valid Directory directly:
object(Directory)#%d (0) {
["path"]=>
uninitialized(string)
["handle"]=>
uninitialized(mixed)
}
Unable to find my handle property
Loading