[php-src] PHP-8.4: ext/zlib: fix gzwrite() not reporting write errors

From: Date: Thu, 26 Nov 2026 19:05:12 +0000
Subject: [php-src] PHP-8.4: ext/zlib: fix gzwrite() not reporting write errors
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-144238@lists.php.net to get a copy of this message
Author: lacatoire (lacatoire) Committer: David Carlier (devnexen) Date: 2026-11-26T19:00:49Z Commit: https://github.com/php/php-src/commit/4476da4fda8f06ecdfef31c48b51821280af51e8 Raw diff: https://github.com/php/php-src/commit/4476da4fda8f06ecdfef31c48b51821280af51e8.diff ext/zlib: fix gzwrite() not reporting write errors 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. Close GH-25697 Changed paths: A ext/zlib/tests/gzwrite_write_error.phpt M NEWS M ext/zlib/zlib_fopen_wrapper.c Diff: diff --git a/NEWS b/NEWS index c71a4275fa11..e42393462d83 100644 --- a/NEWS +++ b/NEWS @@ -265,6 +265,10 @@ PHP NEWS . Fixed a crash in XSLTProcessor::transformToDoc() when the return class cannot be instantiated. (Ilia Alshanetsky) +- Zlib: + . Fixed gzwrite() and fwrite() on a compress.zlib:// stream reporting a + failed write as a successful write of 0 bytes. (lacatoire) + 19 Nov 2026, PHP 8.4.28 - Core: diff --git a/ext/zlib/tests/gzwrite_write_error.phpt b/ext/zlib/tests/gzwrite_write_error.phpt new file mode 100644 index 000000000000..c496317db9ce --- /dev/null +++ b/ext/zlib/tests/gzwrite_write_error.phpt @@ -0,0 +1,30 @@ +--TEST-- +gzwrite(), fwrite() on compress.zlib://: a write error is reported +--EXTENSIONS-- +zlib +--SKIPIF-- +<?php +if (!file_exists('/dev/full')) die('skip needs /dev/full'); +?> +--FILE-- +<?php +$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)); +?> +--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 da948af37ffc..a30ae2fa1690 100644 --- a/ext/zlib/zlib_fopen_wrapper.c +++ b/ext/zlib/zlib_fopen_wrapper.c @@ -64,17 +64,28 @@ 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) { + 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; } total_written += written;

« previous php.cvs (#144238) next »