Req #81049 [Opn->Wfx]: PDO Sqlite in memory database ignores memory_limit

From: Date: Tue, 08 Jun 2021 14:16:03 +0000
Subject: Req #81049 [Opn->Wfx]: PDO Sqlite in memory database ignores memory_limit
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-234266@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81049&edit=1 ID: 81049 Updated by: nikic@php.net Reported by: mvorisek at mvorisek dot cz Summary: PDO Sqlite in memory database ignores memory_limit -Status: Open +Status: Wont fix Type: Feature/Change Request Package: PDO SQLite Operating System: Windows PHP Version: 7.4.19 Block user comment: N Private report: N New Comment: Based on the discussion in https://github.com/php/php-src/pull/7087, we unfortunately cannot use ZendMM with the sqlite3 custom allocator API, because the allocator is not per-connection, and persistent connections cannot use the per-request allocator. Previous Comments: ------------------------------------------------------------------------ [2021-05-19 11:37:14] cmb@php.net > PDO Sqlite should allocate memory using PHP allocation routines SQLite3 offers custom allocators[1] as of 3.6.10, and PHP 7.4 already requires 3.7.5, so this looks possible and would make sense. I don't think not using ZendMM here qualifies as bug, though, since this is quite common for many extensions interfacing with external libraries. Thus, changing to feature request. Note that ext/sqlite3 likely has the same issue. [1] <https://www.sqlite.org/c3ref/mem_methods.html> ------------------------------------------------------------------------ [2021-05-19 11:10:34] mvorisek at mvorisek dot cz Description: ------------ Currently, PDO Sqlite in memory database ignores memory_limit which is dangerous as system's memory can be exhausted by a single script. PDO Sqlite should allocate memory using PHP allocation routines so the memory is counted and limited. Test script: --------------- <?php var_dump(memory_get_usage()); var_dump(memory_get_usage(true)); echo "\n"; for ($i = 0; $i < 3; $i++) { $db = new PDO('sqlite::memory:'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->exec('CREATE TABLE foo (bar TEXT)'); for ($j = 0; $j < 20; $j++) { $db->exec("INSERT INTO foo (bar) VALUES ('" . bin2hex(random_bytes(2 * 1024 * 1024)) . "')"); } var_dump(memory_get_usage()); var_dump(memory_get_usage(true)); $db->exec('DROP TABLE foo'); $db = null; var_dump(memory_get_usage()); var_dump(memory_get_usage(true)); echo "\n"; } Expected result: ---------------- memory_get_usage includes memory allocated by sqlite and memory_limit is honored Actual result: -------------- int(351144) int(2097152) int(351536) int(2097152) int(351176) int(2097152) int(351536) int(2097152) int(351176) int(2097152) int(351536) int(2097152) int(351176) int(2097152) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=81049&edit=1

« previous php.bugs (#234266) next »