From 01fc2ba56661fd414cd3f6e9e85059bd3acdc955 Mon Sep 17 00:00:00 2001 From: Tiago Cruz <153190376+TiagoCruz1224@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:47:27 +0000 Subject: [PATCH] Fix GH-24256: fclose() returns false for stream wrappers without stream_flush() Since the fix for bug #60110, fclose(), file_put_contents() and copy() return the result of the flush on close. A user stream wrapper without stream_flush() made that flush fail, so these functions returned false although nothing went wrong. stream_flush() is optional, so when the stream is closed a missing one now means there was nothing to flush. An explicit fflush() still returns false for such a wrapper, and a stream_flush() that returns false still makes fclose() return false. Based on GH-24259 by lazerg. --- NEWS | 2 + ext/standard/tests/file/gh24256.phpt | 56 ++++++++++++++++++++++++++++ main/streams/userspace.c | 8 +++- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 ext/standard/tests/file/gh24256.phpt 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);