Skip to content

Commit 824c389

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix GH-20620: bzcompress() overflow on large source size.
2 parents 9ed85aa + d9032b3 commit 824c389

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

ext/bz2/bz2.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,15 @@ PHP_FUNCTION(bzcompress)
476476
+ .01 x length of data + 600 which is the largest size the results of the compression
477477
could possibly be, at least that's what the libbz2 docs say (thanks to jeremy@nirvani.net
478478
for pointing this out). */
479-
// TODO Check source string length fits in unsigned int
480-
dest_len = (unsigned int) (source_len + (0.01 * source_len) + 600);
479+
size_t chunk_len = source_len + source_len / 100 + 600;
480+
const size_t min = MIN(ZSTR_MAX_LEN, UINT_MAX);
481+
482+
if (chunk_len < source_len || chunk_len > min) {
483+
zend_argument_value_error(1, "must have a length less than or equal to %zu", min);
484+
RETURN_THROWS();
485+
}
486+
487+
dest_len = (unsigned int) chunk_len;
481488

482489
/* Allocate the destination buffer */
483490
dest = zend_string_alloc(dest_len, 0);

ext/bz2/tests/gh20620.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug GH-20620 (bzcompress with large source)
3+
--EXTENSIONS--
4+
bz2
5+
--SKIPIF--
6+
<?php
7+
if (PHP_INT_SIZE != 8) die('skip this test is for 64bit platforms only');
8+
if (getenv('SKIP_SLOW_TESTS')) die('skip slow tests excluded by request');
9+
?>
10+
--INI--
11+
memory_limit=-1
12+
--FILE--
13+
<?php
14+
try {
15+
bzcompress(str_repeat('1', 4295163906));
16+
} catch (\ValueError $e) {
17+
echo $e->getMessage(), PHP_EOL;
18+
}
19+
?>
20+
--EXPECTF--
21+
bzcompress(): Argument #1 ($data) must have a length less than or equal to %d

0 commit comments

Comments
 (0)