Skip to content

Fix zlib support for large files #17775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
49 changes: 40 additions & 9 deletions ext/zlib/zlib_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,55 @@ struct php_gz_stream_data_t {
static ssize_t php_gziop_read(php_stream *stream, char *buf, size_t count)
{
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
int read;
ssize_t total_read = 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.
*/
do {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With stream_wrapper_register it should be possible to test large file.

  • generate/stream data with some deterministic pattern like 1234...
  • gzip encode them
  • gzip decode them
  • test if the read data matches the expected data/pattern

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, but we would still need #14895 first to properly check for the memory limit

unsigned int chunk_size = MIN(count, INT_MAX);
int read = gzread(self->gz_file, buf, chunk_size);
count -= chunk_size;

if (gzeof(self->gz_file)) {
stream->eof = 1;
}

/* XXX this needs to be looped for the case count > UINT_MAX */
read = gzread(self->gz_file, buf, count);
if (UNEXPECTED(read < 0)) {
return read;
}

if (gzeof(self->gz_file)) {
stream->eof = 1;
}
total_read += read;
buf += read;
} while (count > 0 && !stream->eof);

return read;
return total_read;
}

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.
*/
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;
}

total_written += written;
buf += written;
} while (count > 0);

/* XXX this needs to be looped for the case count > UINT_MAX */
return gzwrite(self->gz_file, (char *) buf, count);
return total_written;
}

static int php_gziop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
Expand Down
Loading