php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75838 Memory leak in pg_escape_bytea()
Submitted: 2018-01-17 22:58 UTC Modified: -
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: ard_1 at mail dot ru Assigned:
Status: Closed Package: PostgreSQL related
PHP Version: 7.0.27 OS: Unix
Private report: No CVE-ID: None
 [2018-01-17 22:58 UTC] ard_1 at mail dot ru
Description:
------------
There is a memory leak in pg_escape_bytea() function due to a missing call to PQfreemem().
This issue was first introduced with the following commit:
https://github.com/php/php-src/commit/1ff7c7b9a31bd3242b06436740322f7246a708f0
so all 7.x are affected. 

Test script:
---------------
<?php
define('LN', "\n");

// Big file ~50Mb
$messData = file_get_contents('./bigfile');

for($i=0; $i<3; $i++) {

	$db = pg_connect('host=pgserver port=5432 dbname=testdb user=dbuser password=dbuserpass connect_timeout=5');
	echo "$i Before: ".memStat().LN;
	$escData = pg_escape_bytea($db, $messData);
	echo "$i Esc: ".memStat().LN;
	unset ($escData);
	pg_close($db);
	echo "$i Clear: ".memStat().LN.LN;

}

echo 'End: '.memStat().LN;

sleep (20);


function memStat() {
	return round(memory_get_usage()/1024/1024, 2).' Mb';
}
?>

Actual result:
--------------
Though memory usage reported by memStat() stays the same, the memory usage reported by "top" or "ps" tools will grow.
Also the issue can be reproduced by running the test script with php-fpm and monitoring the OS memory usage.

The patch for this issue should be trivial - one can invoke PQfreemem(to) after
RETVAL_STRINGL(to, to_len-1); /* to_len includes addtional '\0' */ 
in ext/pgsql/pgsql.c or call efree(to) as it being done for pg_unescape_bytea() function.

Patches

pg_escape_bytea.diff (last revision 2018-01-17 23:41 UTC by ard_1 at mail dot ru)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-01-18 02:54 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=fb205020ce2dd97d777e09d2debf38a01075d370
Log: Fixed #75838 (Memory leak in pg_escape_bytea())
 [2018-01-18 02:54 UTC] laruence@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sat Dec 15 18:01:26 2018 UTC