php.net |  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: 2021-07-18 04:22 UTC
Votes:7
Avg. Score:4.6 ± 0.7
Reproduced:5 of 5 (100.0%)
Same Version:2 (40.0%)
Same OS:2 (40.0%)
From: anestis at fraganestis dot eu Assigned: cmb (profile)
Status: No Feedback 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
Description:
------------
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);
	finfo_close($h);
	return $mime;
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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);
finfo_close($h);
 [2020-02-13 00:46 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2020-02-13 00:46 UTC] requinix@php.net
@anestis: How large is $buffer, and are its contents valid (not corrupted) for the file type?
 [2020-02-13 00:47 UTC] requinix@php.net
@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
@requinix@php.net: just over 17M (17318320 bytes).
 [2020-02-13 05:15 UTC] anestis at fraganestis dot eu
@requinix@php.net: It's a pdf file. Seems legit. The reader (foxit) opens it just fine.
 [2020-02-13 08:15 UTC] nikic@php.net
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: https://3v4l.org/M3YLG
Memory usage of finfo_buffer: https://3v4l.org/LNNoV

On a 16M file, finfo_file uses 20M memory. finfo_buffer, however, uses 244M!
 [2021-07-06 16:22 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2021-07-06 16:22 UTC] cmb@php.net
> 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.

Has anybody reported that upstream[1]?

[1] <http://darwinsys.com/file/>
 [2021-07-18 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Jul 27 02:01:24 2021 UTC