php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81049 PDO Sqlite in memory database ignores memory_limit
Submitted: 2021-05-19 11:10 UTC Modified: 2021-06-08 14:16 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: mvorisek at mvorisek dot cz Assigned:
Status: Wont fix Package: PDO SQLite
PHP Version: 7.4.19 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
5 + 18 = ?
Subscribe to this entry?

 
 [2021-05-19 11:10 UTC] 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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-19 11:37 UTC] cmb@php.net
-Type: Bug +Type: Feature/Change Request
 [2021-05-19 11:37 UTC] 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-06-08 14:16 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2021-06-08 14:16 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Sep 23 14:03:43 2021 UTC