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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ext/bz2/bz2.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,18 @@ PHP_FUNCTION(bzcompress)
RETURN_THROWS();
}

size_t chunk_len = source_len + (0.01 * source_len) + 600;

if (chunk_len < source_len || chunk_len > UINT_MAX) {
zend_argument_value_error(1, "must be less than or equal to %lu", UINT_MAX);
RETURN_THROWS();
}

/* Assign them to easy to use variables, dest_len is initially the length of the data
+ .01 x length of data + 600 which is the largest size the results of the compression
could possibly be, at least that's what the libbz2 docs say (thanks to jeremy@nirvani.net
for pointing this out). */
dest_len = (unsigned int) (source_len + (0.01 * source_len) + 600);
dest_len = (unsigned int) chunk_len;

/* Allocate the destination buffer */
dest = zend_string_alloc(dest_len, 0);
Expand Down
18 changes: 18 additions & 0 deletions ext/bz2/tests/gh20620.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Bug GH-20620 (bzcompress with large source)
--EXTENSIONS--
bz2
--SKIPIF--
<?php if (PHP_INT_SIZE != 8) die('skip this test is for 64bit platforms only'); ?>
--INI--
memory_limit=-1
--FILE--
<?php
try {
bzcompress(str_repeat('1', 4295163906));
} catch (\ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
?>
--EXPECTF--
bzcompress(): Argument #1 ($data) must be less than or equal to %d
Loading