From:             tim at digicol dot de
Operating system: SuSE Linux 8.2 (i586)
PHP version:      4.3.8
PHP Bug Type:     Strings related
Bug description:  Memory leak in sprintf argument swapping

Description:
------------
Using argument swapping in sprintf() or printf() calls makes PHP leak
memory. (I've been running this code using the PHP CLI binary.)

Have been able to reproduce this with PHP 4.3.7 as well.

./configure --with-config-file-path=/usr/local/php/conf --enable-cli
--enable-track-vars --disable-magic-quotes --enable-xml --enable-sysvsem
--enable-sysvshm --with-apxs=/usr/local/apache/bin/apxs --disable-debug
--enable-ftp --enable-wddx --enable-sigchild --enable-inline-optimization
--enable-exif --with-pgsql=/usr/local/pgsql --prefix=/usr/local/php
--with-oci8=/dot/oracle/product/9.2.0 --with-db3=/usr/local/db3
--with-zlib --with-dom --with-gd --with-mhash=/usr/local/mhash
--enable-mbstring=all --enable-mbstr-enc-trans --enable-mbregex
--disable-pic --with-gdbm --enable-pcntl --enable-sysvmsg
--with-iconv=/dot/local/iconv --enable-memory-limit

Reproduce code:
---------------
<?php

$b = memory_get_usage();

$location = "zoo";
$num = 2;

// No memory leak:
// $format = "The %s contains %s monkeys\n";

// Memory leak:
$format = "The %2\$s contains %1\$d monkeys\n";

for ($i = 1; $i <= 100000; $i++)
  sprintf($format, $num, $location);

echo "Grown by " . (memory_get_usage() - $b) . "\n";

?>


Expected result:
----------------
Grown by 376

Actual result:
--------------
Grown by 4000336

-- 
Edit bug report at http://bugs.php.net/?id=29838&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=29838&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=29838&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=29838&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=29838&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=29838&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=29838&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=29838&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=29838&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=29838&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=29838&r=notwrong
Not enough info:             http://bugs.php.net/fix.php?id=29838&r=notenoughinfo
Submitted twice:             http://bugs.php.net/fix.php?id=29838&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=29838&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=29838&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=29838&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=29838&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=29838&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=29838&r=float

Reply via email to