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 @@ -229,6 +229,8 @@ PHP NEWS
. Fixed use-after-free of the previous request's default stream context when
a stream error is raised during request startup, such as when a POST body
cannot be buffered to a temporary file. (Go Kudo)
. Fixed bug GH-24256 (fclose() returns false for stream wrappers without
stream_flush()). (lazerg, Tiago Cruz)
. Fixed bug GH-24175 (Use-after-free when pclose() closes a stream from its
user filter). (Edmond)
. Fixed bug GH-15701 (Build failure: incompatible funopen() seeker pointer
Expand Down
56 changes: 56 additions & 0 deletions ext/standard/tests/file/gh24256.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
GH-24256 (fclose() returns false for stream wrappers without stream_flush())
--FILE--
<?php
class WrapperWithoutFlush {
public $context;

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

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

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

class WrapperWithFailingFlush extends WrapperWithoutFlush {
public function stream_flush() {
return false;
}
}

stream_wrapper_register('noflush', WrapperWithoutFlush::class);
stream_wrapper_register('failingflush', WrapperWithFailingFlush::class);

echo "Without stream_flush():\n";
$fp = fopen('noflush://foo', 'w');
var_dump(fwrite($fp, 'data'));
var_dump(fflush($fp));
var_dump(fwrite($fp, 'data'));
var_dump(fclose($fp));
var_dump(file_put_contents('noflush://foo', 'data'));
var_dump(copy(__FILE__, 'noflush://foo'));

echo "With a failing stream_flush():\n";
$fp = fopen('failingflush://foo', 'w');
var_dump(fwrite($fp, 'data'));
var_dump(fclose($fp));
var_dump(file_put_contents('failingflush://foo', 'data'));
?>
--EXPECT--
Without stream_flush():
int(4)
bool(false)
int(4)
bool(true)
int(4)
bool(true)
With a failing stream_flush():
int(4)
bool(false)
bool(false)
8 changes: 7 additions & 1 deletion main/streams/userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,13 @@ static int php_userstreamop_flush(php_stream *stream)
stream->flags &= ~PHP_STREAM_FLAG_NO_FCLOSE;
stream->flags |= orig_no_fclose;

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: when closing, a wrapper without it has nothing to flush */
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