Skip to content

Commit 2a9a521

Browse files
committed
Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4: fileinfo: Fixed bug #78987 High memory usage during encoding detection
2 parents 01cfd5e + fa96c7e commit 2a9a521

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

ext/fileinfo/libmagic/encoding.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ file_encoding(struct magic_set *ms, const struct buffer *b, unicodechar **ubuf,
7070
size_t *ulen, const char **code, const char **code_mime, const char **type)
7171
{
7272
const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
73-
size_t nbytes = b->flen;
73+
size_t nbytes = b->flen > 64*1024 ? 64*1024 : b->flen;
7474
size_t mlen;
7575
int rv = 1, ucs_type;
7676
unsigned char *nbuf = NULL;

ext/fileinfo/tests/bug78987.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Bug #78987 High memory usage during encoding detection
3+
--SKIPIF--
4+
<?php require_once(__DIR__ . '/skipif.inc'); ?>
5+
--INI--
6+
memory_limit=512M
7+
--FILE--
8+
<?php
9+
$finfo = new finfo(FILEINFO_MIME_TYPE);
10+
$minSize = 128 * 1024;
11+
$maxSize = 16 * 1024 * 1024;
12+
13+
$map = array(
14+
131072 => 2097152,
15+
262144 => 2097152,
16+
524288 => 2097152,
17+
1048576 => 4194304,
18+
2097152 => 6295552,
19+
4194304 => 10493952,
20+
8388608 => 16785408,
21+
16777216 => 29368320,
22+
);
23+
for($size = $minSize; $size <= $maxSize; $size *= 2) {
24+
$content = str_repeat('0', $size);
25+
26+
$finfo->buffer($content);
27+
28+
$m = memory_get_peak_usage(true);
29+
printf("%-8d => %s\n", $size, $m <= $map[$size] ? "ok" : "$m");
30+
}
31+
?>
32+
--EXPECT--
33+
131072 => ok
34+
262144 => ok
35+
524288 => ok
36+
1048576 => ok
37+
2097152 => ok
38+
4194304 => ok
39+
8388608 => ok
40+
16777216 => ok

ext/fileinfo/tests/finfo_file_basic.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ try {
2424
*** Testing finfo_file() : basic functionality ***
2525
string(28) "text/x-php; charset=us-ascii"
2626
string(22) "PHP script, ASCII text"
27-
string(25) "text/plain; charset=utf-8"
27+
string(28) "text/plain; charset=us-ascii"
2828
finfo_file(): Argument #1 ($finfo) must not contain any null bytes

0 commit comments

Comments
 (0)