Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions ext/zlib/tests/gzwrite_write_error.phpt
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 16 additions & 5 deletions ext/zlib/zlib_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading