|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79263 finfo_buffer fails with "PHP Fatal error: Allowed memory size of"
Submitted: 2020-02-12 07:08 UTC Modified: 2020-02-14 17:14 UTC
Avg. Score:4.5 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: anestis at fraganestis dot eu Assigned:
Status: Open Package: Unknown/Other Function
PHP Version: 7.4.2 OS: Debian 9
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-02-12 07:08 UTC] anestis at fraganestis dot eu
finfo_buffer for the contents of a specific file (which I can provide but not make it completely public) fails with

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 138546568 bytes)

Test script:
function getBufferMime($buffer)
	$h = finfo_open(FILEINFO_MIME);
	$mime = finfo_buffer($h, $buffer);
	return $mime;


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-02-12 23:45 UTC] KseniMailwow at gmail dot com
Use memory_limit (in ini) to increase the memory limit. This is not a bug.
 [2020-02-12 23:55 UTC] bugreports at gmail dot com
or don't pass around whole files in strings which allocates memory often two or even three times

$h = finfo_open();
$mime_type = finfo_file($h, $path, FILEINFO_MIME_TYPE);
 [2020-02-13 00:46 UTC]
-Status: Open +Status: Feedback
 [2020-02-13 00:46 UTC]
@anestis: How large is $buffer, and are its contents valid (not corrupted) for the file type?
 [2020-02-13 00:47 UTC]
@bugreports: Passing strings around does not allocate new memory because of copy-on-write.
 [2020-02-13 05:11 UTC] anestis at fraganestis dot eu
-Status: Feedback +Status: Open
 [2020-02-13 05:11 UTC] anestis at fraganestis dot eu just over 17M (17318320 bytes).
 [2020-02-13 05:15 UTC] anestis at fraganestis dot eu It's a pdf file. Seems legit. The reader (foxit) opens it just fine.
 [2020-02-13 08:15 UTC]
This sounds similar to bug #78987. In any case, this is a bug in upstream libmagic, it needs to be reported and fixed there before we can do anything about it.
 [2020-02-14 17:14 UTC] anestis at fraganestis dot eu
The error is produced when I use the following sequence:
1) read the data from a temp file into a buffer
2) call finfo_buffer to get the mimetype
3) parse the buffer

The error is not produced and everything works fine if the sequence is modified as follows:
1) call finfo_file on temp file to get the mimetype
2) read the data from the same temp file into a buffer
3) parse the buffer
 [2020-06-12 07:47 UTC] magnar at myrtveit dot com
I've created two scripts illustrating the problem with finfo_buffer:

Memory usage of finfo_file:
Memory usage of finfo_buffer:

On a 16M file, finfo_file uses 20M memory. finfo_buffer, however, uses 244M!
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue May 18 11:01:24 2021 UTC