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

Fix GH-7910: rename fails on Windows if the target is being executed #9351

Draft
wants to merge 2 commits into
base: PHP-8.1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions Zend/tests/gh7910.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
GH-7910 (rename fails on Windows if the target is being executed)
--FILE--
<?php
$content = file_get_contents(__FILE__);
file_put_contents(__DIR__ . "/gh7910.php.bak", $content);
var_dump(rename(__DIR__ . "/gh7910.php.bak", __FILE__));
?>
--CLEAN--
<?php
@unlink(__DIR__ . "/gh7910.php.bak");
?>
--EXPECT--
bool(true)
1 change: 1 addition & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,7 @@ ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count
if (file_handle->opened_path) {
zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);
}
zend_stream_close(file_handle);
if (op_array) {
zend_execute(op_array, retval);
zend_exception_restore();
Expand Down
10 changes: 10 additions & 0 deletions Zend/zend_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ ZEND_API zend_result zend_stream_fixup(zend_file_handle *file_handle, char **buf
return SUCCESS;
} /* }}} */

/* Closes handle stream if opened by zend_stream_fixup() */
ZEND_API void zend_stream_close(zend_file_handle *fh) /* {{{ */
{
if (fh->type == ZEND_HANDLE_STREAM && fh->handle.stream.handle
&& fh->handle.stream.closer == (zend_stream_closer_t)zend_stream_stdio_closer) {
fh->handle.stream.closer(fh->handle.stream.handle);
fh->handle.stream.closer = NULL;
}
} /* }}} */

static void zend_file_handle_dtor(zend_file_handle *fh) /* {{{ */
{
switch (fh->type) {
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ ZEND_API void zend_stream_init_filename(zend_file_handle *handle, const char *fi
ZEND_API void zend_stream_init_filename_ex(zend_file_handle *handle, zend_string *filename);
ZEND_API zend_result zend_stream_open(zend_file_handle *handle);
ZEND_API zend_result zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t *len);
ZEND_API void zend_stream_close(zend_file_handle *handle);
ZEND_API void zend_destroy_file_handle(zend_file_handle *file_handle);

void zend_stream_init(void);
Expand Down