Skip to content

Commit 09d3b73

Browse files
committed
Resources should be closed during object destructioin, not during freeing.
1 parent b41d715 commit 09d3b73

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

ext/spl/spl_directory.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,36 @@ static void spl_filesystem_file_free_line(spl_filesystem_object *intern) /* {{{
7272
}
7373
} /* }}} */
7474

75+
static void spl_filesystem_object_destroy_object(zend_object *object) /* {{{ */
76+
{
77+
spl_filesystem_object *intern = spl_filesystem_from_obj(object);
78+
79+
zend_objects_destroy_object(object);
80+
81+
switch(intern->type) {
82+
case SPL_FS_DIR:
83+
if (intern->u.dir.dirp) {
84+
php_stream_close(intern->u.dir.dirp);
85+
intern->u.dir.dirp = NULL;
86+
}
87+
break;
88+
case SPL_FS_FILE:
89+
if (intern->u.file.stream) {
90+
/*
91+
if (intern->u.file.zcontext) {
92+
zend_list_delref(Z_RESVAL_P(intern->zcontext));
93+
}
94+
*/
95+
if (!intern->u.file.stream->is_persistent) {
96+
php_stream_close(intern->u.file.stream);
97+
} else {
98+
php_stream_pclose(intern->u.file.stream);
99+
}
100+
}
101+
break;
102+
}
103+
} /* }}} */
104+
75105
static void spl_filesystem_object_free_storage(zend_object *object) /* {{{ */
76106
{
77107
spl_filesystem_object *intern = spl_filesystem_from_obj(object);
@@ -92,26 +122,12 @@ static void spl_filesystem_object_free_storage(zend_object *object) /* {{{ */
92122
case SPL_FS_INFO:
93123
break;
94124
case SPL_FS_DIR:
95-
if (intern->u.dir.dirp) {
96-
php_stream_close(intern->u.dir.dirp);
97-
intern->u.dir.dirp = NULL;
98-
}
99125
if (intern->u.dir.sub_path) {
100126
efree(intern->u.dir.sub_path);
101127
}
102128
break;
103129
case SPL_FS_FILE:
104130
if (intern->u.file.stream) {
105-
/*
106-
if (intern->u.file.zcontext) {
107-
zend_list_delref(Z_RESVAL_P(intern->zcontext));
108-
}
109-
*/
110-
if (!intern->u.file.stream->is_persistent) {
111-
php_stream_close(intern->u.file.stream);
112-
} else {
113-
php_stream_pclose(intern->u.file.stream);
114-
}
115131
if (intern->u.file.open_mode) {
116132
efree(intern->u.file.open_mode);
117133
}
@@ -3108,7 +3124,7 @@ PHP_MINIT_FUNCTION(spl_directory)
31083124
spl_filesystem_object_handlers.clone_obj = spl_filesystem_object_clone;
31093125
spl_filesystem_object_handlers.cast_object = spl_filesystem_object_cast;
31103126
spl_filesystem_object_handlers.get_debug_info = spl_filesystem_object_get_debug_info;
3111-
spl_filesystem_object_handlers.dtor_obj = zend_objects_destroy_object;
3127+
spl_filesystem_object_handlers.dtor_obj = spl_filesystem_object_destroy_object;
31123128
spl_filesystem_object_handlers.free_obj = spl_filesystem_object_free_storage;
31133129
spl_ce_SplFileInfo->serialize = zend_class_serialize_deny;
31143130
spl_ce_SplFileInfo->unserialize = zend_class_unserialize_deny;

0 commit comments

Comments
 (0)