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-- + +--FILE-- + +--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;