Skip to content

Better size check in bzdecompress #19069

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 3 additions & 7 deletions ext/bz2/bz2.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,7 @@ PHP_FUNCTION(bzdecompress)
size_t source_len;
int error;
bool small = 0;
#ifdef PHP_WIN32
unsigned __int64 size = 0;
#else
unsigned long long size = 0;
#endif
bz_stream bzs;

if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &source, &source_len, &small)) {
Expand All @@ -524,7 +520,7 @@ PHP_FUNCTION(bzdecompress)
/* compression is better then 2:1, need to allocate more memory */
bzs.avail_out = source_len;
size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
#ifndef ZEND_ENABLE_ZVAL_LONG64
#if SIZEOF_LONG_LONG > SIZEOF_SIZE_T
if (size > SIZE_MAX) {
/* no reason to continue if we're going to drop it anyway */
break;
Expand All @@ -536,9 +532,9 @@ PHP_FUNCTION(bzdecompress)

if (error == BZ_STREAM_END || error == BZ_OK) {
size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
#ifndef ZEND_ENABLE_ZVAL_LONG64
#if SIZEOF_LONG_LONG > SIZEOF_SIZE_T
if (UNEXPECTED(size > SIZE_MAX)) {
php_error_docref(NULL, E_WARNING, "Decompressed size too big, max is %zd", SIZE_MAX);
php_error_docref(NULL, E_WARNING, "Decompressed size too big, max is %zu", SIZE_MAX);
zend_string_efree(dest);
RETVAL_LONG(BZ_MEM_ERROR);
} else
Expand Down