Skip to content

Fix gzwrite() not reporting write errors - #25697

Closed
lacatoire wants to merge 2 commits into
php:PHP-8.4from
lacatoire:fix/zlib-gzwrite-write-error-PHP-8.5
Closed

lacatoire wants to merge 2 commits into
php:PHP-8.4from
lacatoire:fix/zlib-gzwrite-write-error-PHP-8.5

Conversation

@lacatoire

@lacatoire lacatoire commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

gzwrite() and fwrite() on a compress.zlib:// stream reported success with 0 bytes when zlib failed to write (full disk, I/O error): the stream wrapper tested for a negative return, but gzwrite() returns 0 on error. The error is now reported when zlib sets Z_ERRNO, so the write returns false with a notice. A plain 0 on a stream opened for reading is unchanged.

To reproduce:

$data = random_bytes(300000);
$w = gzopen('/dev/full', 'w');
var_dump(gzwrite($w, $data));
$f = fopen('compress.zlib:///dev/full', 'w');
var_dump(fwrite($f, $data));

Before (8.4.26 and 8.5.11):

int(0)
int(0)

After:

Notice: gzwrite(): Write of 300000 bytes failed with errno=28 No space left on device in ... on line 4
bool(false)

Notice: fwrite(): Write of 300000 bytes failed with errno=28 No space left on device in ... on line 6
bool(false)

@devnexen

Copy link
Copy Markdown
Member

so if it reproduces on 8.4, why did you target 8.5 ?

Comment thread ext/zlib/zlib_fopen_wrapper.c Resolved
@lacatoire

Copy link
Copy Markdown
Member Author

so if it reproduces on 8.4, why did you target 8.5 ?

good question, I don't remember, changing it

gzwrite() returns 0 on error, never a negative value, so the
compress.zlib:// wrapper reported a failed write as a successful write
of 0 bytes. Return -1 when zlib reports an error and nothing was written.
@lacatoire
lacatoire force-pushed the fix/zlib-gzwrite-write-error-PHP-8.5 branch from 8714249 to b542e9d Compare November 25, 2026 19:35
@lacatoire
lacatoire changed the base branch from PHP-8.5 to PHP-8.4 November 25, 2026 19:35
@devnexen

Copy link
Copy Markdown
Member

seems there is regression information wise.

diff --git a/ext/zlib/zlib_fopen_wrapper.c b/ext/zlib/zlib_fopen_wrapper.c
index 04dd7fa93dc..a30ae2fa169 100644
--- a/ext/zlib/zlib_fopen_wrapper.c
+++ b/ext/zlib/zlib_fopen_wrapper.c
@@ -78,6 +78,11 @@ static ssize_t php_gziop_write(php_stream *stream, const char *buf, size_t count

                        gzerror(self->gz_file, &error);
                        if (error != Z_OK && total_written == 0) {
+                               if (error == Z_ERRNO && !(stream->flags & PHP_STREAM_FLAG_SUPPRESS_ERRORS)) {
+                                       char errstr[256];
+                                       php_error_docref(NULL, E_NOTICE, "Write of %u bytes failed with errno=%d %s",
+                                                       chunk_size, errno, php_socket_strerror_s(errno, errstr, sizeof(errstr)));
+                               }
                                return -1;
                        }
                        return total_written;
diff --git a/ext/zlib/tests/gzwrite_write_error.phpt b/ext/zlib/tests/gzwrite_write_error.phpt
index 0925eb2c17b..c496317db9c 100644
--- a/ext/zlib/tests/gzwrite_write_error.phpt
+++ b/ext/zlib/tests/gzwrite_write_error.phpt
@@ -11,9 +11,20 @@
 $data = random_bytes(300000);
 $w = gzopen('/dev/full', 'w');
 var_dump(gzwrite($w, $data));
+var_dump(gzwrite($w, 'x'));
 $f = fopen('compress.zlib:///dev/full', 'w');
 var_dump(fwrite($f, $data));
+var_dump(file_put_contents('compress.zlib:///dev/full', $data));
 ?>
---EXPECT--
+--EXPECTF--
+Notice: gzwrite(): Write of 300000 bytes failed with errno=%d %s in %s on line %d
 bool(false)
+
+Notice: gzwrite(): Write of 1 bytes failed with errno=%d %s in %s on line %d
+bool(false)
+
+Notice: fwrite(): Write of 300000 bytes failed with errno=%d %s in %s on line %d
+bool(false)
+
+Notice: file_put_contents(): Write of 300000 bytes failed with errno=%d %s in %s on line %d
 bool(false)

@devnexen devnexen closed this in 4476da4 Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants