Skip to content
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ PHP NEWS
when getpwnam_r() needs a larger buffer. (Ilia Alshanetsky)
. Fixed password_get_info() and password_needs_rehash() accepting malformed
bcrypt costs. (Ilia Alshanetsky)
. Fixed bug GH-24256 (fclose() returns false for stream wrappers without
stream_flush()). (Lazizbek Ergashev)

- Zip:
. Fixed use-after-free when re-entering ZipArchive during destruction or
Expand Down
34 changes: 34 additions & 0 deletions ext/standard/tests/file/gh24256.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
GH-24256 (fclose() returns false for stream wrappers without stream_flush())
--FILE--
<?php
class TestStream {
public $context;

function stream_open($path, $mode, $options, &$opened_path) {
return true;
}

function stream_write($data) {
return strlen($data);
}

function url_stat($path, $flags) {
return false;
}
}

stream_wrapper_register('test', TestStream::class);

$fp = fopen('test://foo', 'w');
var_dump(fwrite($fp, 'data'));
var_dump(fclose($fp));

var_dump(file_put_contents('test://foo', 'data'));
var_dump(copy(__FILE__, 'test://foo'));
?>
--EXPECT--
int(4)
bool(true)
int(4)
bool(true)
8 changes: 7 additions & 1 deletion main/streams/userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,13 @@ static int php_userstreamop_flush(php_stream *stream)
zend_result call_result = zend_call_method_if_exists(Z_OBJ(us->object), func_name, &retval, 0, NULL);
zend_string_release_ex(func_name, false);

int ret = call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF && zend_is_true(&retval) ? 0 : -1;
int ret;
if (call_result == FAILURE) {
/* stream_flush is optional, so a missing one is not an error when closing */
ret = stream->in_free ? 0 : -1;
} else {
ret = Z_TYPE(retval) != IS_UNDEF && zend_is_true(&retval) ? 0 : -1;
}

zval_ptr_dtor(&retval);

Expand Down
Loading