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: 2020-02-14 17:14 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: anestis at fraganestis dot eu
New email:
PHP Version: OS:

 

 [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!
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Oct 25 08:01:24 2020 UTC