diff --git a/NEWS b/NEWS index f2dc4c3302b9..eec84dca62ee 100644 --- a/NEWS +++ b/NEWS @@ -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 diff --git a/ext/standard/tests/file/gh24256.phpt b/ext/standard/tests/file/gh24256.phpt new file mode 100644 index 000000000000..5d6ae4829e23 --- /dev/null +++ b/ext/standard/tests/file/gh24256.phpt @@ -0,0 +1,56 @@ +--TEST-- +GH-24256 (fclose() returns false for stream wrappers without stream_flush()) +--FILE-- + +--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) diff --git a/main/streams/userspace.c b/main/streams/userspace.c index ec8bdcdf0ac3..a8a9fdd6f0c6 100644 --- a/main/streams/userspace.c +++ b/main/streams/userspace.c @@ -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);