Bug #72143 [NEW]: preg_replace uses int instead of size_t on zend_string_allocs

From: Date: Tue, 03 May 2016 12:53:06 +0000
Subject: Bug #72143 [NEW]: preg_replace uses int instead of size_t on zend_string_allocs
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200872@lists.php.net to get a copy of this message
From: burmartke at gmail dot com Operating system: Ubuntu 16.04 LTS PHP version: 7.0.6 Package: PCRE related Bug Type: Bug Bug description:preg_replace uses int instead of size_t on zend_string_allocs Description: ------------ preg_replace() declares its alloc_len as an int, and zend_string_(re)alloc() expects a size_t, so when it tries to allocate a string >= 0x40000000 it gets sign-extended and the alloc fails. Test script: --------------- <?php error_log( "++++++++++++++++++++++++++begin " ); $str = str_repeat( 'a', 0x40000000 ) . 'b'; $len = strlen( $str ); error_log( "len=$len (0x" . dechex( $len ) . ")" . ", 18446744071562068000 (0x" . dechex( 18446744071562068000 ) . ")" ); $out = preg_replace( '/b/', '', $str ); error_log( "++++++++++++++++++++++++++end " ); Expected result: ---------------- A successful replace. ++++++++++++++++++++++++++begin len=1073741825 (0x40000001), 18446744071562068000 (0xffffffff80000000) ++++++++++++++++++++++++++end Actual result: -------------- Either a PHP Fatal error: Out of memory (allocated 1075843072) (tried to allocate 18446744071562068000 bytes) or a segmentation fault. -- Edit bug report at https://bugs.php.net/bug.php?id=72143&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72143&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72143&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72143&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=72143&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=72143&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=72143&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=72143&r=needscript Try newer version: https://bugs.php.net/fix.php?id=72143&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=72143&r=support Expected behavior: https://bugs.php.net/fix.php?id=72143&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=72143&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=72143&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=72143&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72143&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=72143&r=dst IIS Stability: https://bugs.php.net/fix.php?id=72143&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=72143&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=72143&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=72143&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=72143&r=mysqlcfg

« previous php.bugs (#200872) next »