Bug #78612 [NEW]: strtr leaks memory when integer keys are used and the subject string shorter

From: Date: Mon, 30 Sep 2019 12:27:09 +0000
Subject: Bug #78612 [NEW]: strtr leaks memory when integer keys are used and the subject string shorter
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222961@lists.php.net to get a copy of this message
From:             itsoft3g at gmail dot com
Operating system: Ubuntu 18.04.2 LTS
PHP version:      7.3.10
Package:          Strings related
Bug Type:         Bug
Bug description:strtr leaks memory when integer keys are used and the subject string shorter

Description:
------------
Passing 2nd argument as an array to strtr() - replace substrings
Here array keys are integers.

Say an interger (7111222333000001) is the find item, consider it as
string its length is 16.
Say the subject is Hello which length is 5.

In this case, strtr leaks memory.

However, when the subject is longer than the find item, there is no
memory leak.
Also when the find is string there too no memory leak.

Test script:
---------------
$find_replace = array();
$_a = 7111222333000001;//find - consider as string it's length is 16
$_b = 5000001;

for($j=0;$j<10;$j++){	
	$find_replace[$_a + $j] = $_b + $j;
}

function test_strtr($subject, $find_replace){
	$memory_before = memory_get_usage();
	for($i=0;$i<50000;$i++){
		strtr($subject, $find_replace);
	}
	echo round( (memory_get_usage() - $memory_before) /(1024*1024), 4 ).'
MB ';
}

test_strtr('Hello', $find_replace);//subject is shorter then find(length
5), memory leaks
test_strtr('Lorem ipsum dolor', $find_replace);//subject is lengthier
then find(length 17), no memory leak


-- 
Edit bug report at https://bugs.php.net/bug.php?id=78612&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=78612&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=78612&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=78612&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=78612&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=78612&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=78612&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=78612&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=78612&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=78612&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=78612&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=78612&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=78612&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=78612&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=78612&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=78612&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=78612&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=78612&r=mysqlcfg


Thread (3 messages)

« previous php.bugs (#222961) next »