From b542e9df23373a4cb103aa61585d2c52ec875738 Mon Sep 17 00:00:00 2001 From: lacatoire Date: Wed, 25 Nov 2026 20:28:54 +0100 Subject: [PATCH 1/2] Fix gzwrite() not reporting write errors 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. --- ext/zlib/tests/gzwrite_write_error.phpt | 19 +++++++++++++++++++ ext/zlib/zlib_fopen_wrapper.c | 16 +++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 ext/zlib/tests/gzwrite_write_error.phpt diff --git a/ext/zlib/tests/gzwrite_write_error.phpt b/ext/zlib/tests/gzwrite_write_error.phpt new file mode 100644 index 000000000000..0925eb2c17b9 --- /dev/null +++ b/ext/zlib/tests/gzwrite_write_error.phpt @@ -0,0 +1,19 @@ +--TEST-- +gzwrite(), fwrite() on compress.zlib://: a write error is reported +--EXTENSIONS-- +zlib +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(false) +bool(false) diff --git a/ext/zlib/zlib_fopen_wrapper.c b/ext/zlib/zlib_fopen_wrapper.c index da948af37ffc..04dd7fa93dc7 100644 --- a/ext/zlib/zlib_fopen_wrapper.c +++ b/ext/zlib/zlib_fopen_wrapper.c @@ -64,17 +64,23 @@ static ssize_t php_gziop_write(php_stream *stream, const char *buf, size_t count struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract; ssize_t total_written = 0; - /* Despite the count argument of gzread() being "unsigned int", - * the return value is "int". Error returns are values < 0, otherwise the count is returned. - * To properly distinguish error values from success value, we therefore need to cap at INT_MAX. + /* Despite the count argument of gzwrite() being "unsigned int", + * the return value is "int". Error returns are 0, otherwise the count is returned. + * To keep the count representable in the return value, we therefore need to cap at INT_MAX. */ do { unsigned int chunk_size = MIN(count, INT_MAX); int written = gzwrite(self->gz_file, buf, chunk_size); count -= chunk_size; - if (UNEXPECTED(written < 0)) { - return written; + if (UNEXPECTED(written <= 0)) { + int error = Z_OK; + + gzerror(self->gz_file, &error); + if (error != Z_OK && total_written == 0) { + return -1; + } + return total_written; } total_written += written; From a8a31f336a794d439909bb8a4c3d9c71ad7dff8e Mon Sep 17 00:00:00 2001 From: lacatoire Date: Wed, 25 Nov 2026 22:54:07 +0100 Subject: [PATCH 2/2] Emit a notice when gzwrite() fails on a write error --- ext/zlib/tests/gzwrite_write_error.phpt | 13 ++++++++++++- ext/zlib/zlib_fopen_wrapper.c | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ext/zlib/tests/gzwrite_write_error.phpt b/ext/zlib/tests/gzwrite_write_error.phpt index 0925eb2c17b9..c496317db9ce 100644 --- a/ext/zlib/tests/gzwrite_write_error.phpt +++ b/ext/zlib/tests/gzwrite_write_error.phpt @@ -11,9 +11,20 @@ if (!file_exists('/dev/full')) die('skip needs /dev/full'); $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) diff --git a/ext/zlib/zlib_fopen_wrapper.c b/ext/zlib/zlib_fopen_wrapper.c index 04dd7fa93dc7..a30ae2fa1690 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;