|  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: -
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
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:
so all 7.x are affected. 

Test script:
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);
	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.


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


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-01-18 02:54 UTC]
Automatic comment on behalf of
Log: Fixed #75838 (Memory leak in pg_escape_bytea())
 [2018-01-18 02:54 UTC]
-Status: Open +Status: Closed
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC