Skip to content

Commit 7fba8bd

Browse files
mbonneaubwoebi
authored andcommitted
Fixed bug #74240 (deflate_add can allocate too much memory)
1 parent 8be63ce commit 7fba8bd

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ PHP NEWS
2323
. Fixed bug #71003 (Expose MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT to PDO
2424
interface). (Thomas Orozco)
2525

26-
. Streams:
26+
- Streams:
2727
. Fixed bug #74216 (Correctly fail on invalid IP address ports). (Sara)
2828

29+
- Zlib:
30+
. Fixed bug #74240 (deflate_add can allocate too much memory). (Matt Bonneau)
31+
2932
16 Mar 2017 PHP 7.0.17
3033

3134
- Core:

ext/zlib/tests/bug74240.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Bug #74240 (deflate_add can allocate too much memory)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("zlib")) {
6+
print "skip - ZLIB extension not loaded";
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
12+
ini_set('memory_limit', '64M');
13+
14+
$deflator = deflate_init(ZLIB_ENCODING_RAW);
15+
16+
$bytes = str_repeat("*", 65536);
17+
18+
// this crashes after about 500 iterations if PHP is
19+
// configured for 64M
20+
for ($i = 0; $i < 1000; $i++) {
21+
$output = deflate_add(
22+
$deflator,
23+
$bytes,
24+
ZLIB_SYNC_FLUSH
25+
);
26+
}
27+
echo "Completed\n";
28+
?>
29+
--EXPECT--
30+
Completed

ext/zlib/zlib.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,10 +1154,8 @@ PHP_FUNCTION(deflate_add)
11541154
RETURN_EMPTY_STRING();
11551155
}
11561156

1157-
out_size = PHP_ZLIB_BUFFER_SIZE_GUESS(ctx->total_in + in_len);
1158-
out_size = (ctx->total_out >= out_size) ? 16 : (out_size - ctx->total_out);
1159-
out_size = (out_size < 16) ? 16 : out_size;
1160-
out_size += 64;
1157+
out_size = PHP_ZLIB_BUFFER_SIZE_GUESS(in_len);
1158+
out_size = (out_size < 64) ? 64 : out_size;
11611159
out = zend_string_alloc(out_size, 0);
11621160

11631161
ctx->next_in = (Bytef *) in_buf;

0 commit comments

Comments
 (0)