SQLite Forum

Test SQLITE_NOMEM result code on Linux (Memory : 8Gb)
Login

Test SQLITE_NOMEM result code on Linux (Memory : 8Gb)

(1) By anonymous on 2021-09-27 05:10:50

Is there any way to test SQLITE_NOMEM result code on the following interfaces :
1. sqlite_open(:memory:)
2. sqlite_prepare_v2

I am using it in a library and have to simulate for test purpose.

Also please provide any pointers on how to use sqlite_test_control for limiting memory and fault injections.

Sqlite Version : 3.22

(2) By Richard Hipp (drh) on 2021-09-27 10:48:34 in reply to 1 [link]

You can register your own memory allocator using the
[sqlite3_config(SQLITE_CONFIG_MALLOC, ...)[1] interface.  This is what the
various SQLite test harnesses do.  Normally calls to the custom malloc fall
through into system malloc, but it is possible to inject faults.  There
is an example of doing this in the [CLI source code][2].

You might also set the [PRAGMA hard_heap_limit][3] to some small value
in one connection, and then open a separate connection.  The hard_heap_limit
applies to the entire process, not to individual connections.

[1]: https://www.sqlite.org/c3ref/c_config_covering_index_scan.html
[2]: src:/info/9df263dc09496?ln=524-530
[3]: https://sqlite.org/pragma.html#pragma_hard_heap_limit