Repository navigation
fix: dbFetch() raises "out of memory" instead of crashing when SQLite cannot convert a text value - #890
Conversation
…te cannot convert a text value `sqlite3_column_text()` and `sqlite3_column_blob()` return `NULL` when SQLite runs out of memory, for instance while transcoding a large value from a UTF-16 database. With `check_utf8 = FALSE`, `fetch_string()` passed that `NULL` to `Rf_mkCharCE()` and crashed R; with `check_utf8 = TRUE`, `fetch_checked_string()` returned `""` and relied on the next step to fail. The date, datetime and time readers passed the `NULL` on to their parsers. All of them now raise an R error with SQLite's message when the pointer is `NULL` and `sqlite3_errcode()` is `SQLITE_NOMEM`, as the SQLite documentation describes. Empty strings and empty blobs read as `""` as before. The tests cap SQLite's heap through two internal entry points, `sqlite_heap_limits()` and `sqlite_memory_used()`, because `PRAGMA hard_heap_limit` cannot lift the limit again and lowering it also lowers the soft limit for the rest of the process. Closes #889. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7mthffYErGmWTGDfQ
…out of memory A second `dbFetch()` after an out-of-memory error reads the same row again, with `SQLITE_NOMEM` still on the connection. With `check_utf8 = TRUE`, the `NULL` that `sqlite3_column_blob()` returns for an empty blob in an earlier column then raised "Cannot read column <j>: out of memory" for that column. A `NULL` blob with `sqlite3_column_bytes() == 0` is now an empty blob, and a `NULL` text pointer for a NULL value is never an error; the next step reports the out-of-memory error instead. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd7mthffYErGmWTGDfQ
|
Review of this pull request at 2b03480, by a Claude Code reviewer with clean context that read the description, issue #889, the diff against What was run
What holds
HighNone. MediumNone. Low
Generated by Claude Code |
Accepted limitationsOne clean review round (report) found nothing above Low. Its one introduced finding, a fetch after an out-of-memory error blaming an empty blob in an earlier column with
Generated by Claude Code |
Cite only the final SQLite upgrade, and say that the UTF-8 check is opt-in. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd6znpbaJUu6TJLjgQC
…mory The test for an empty blob read again after out of memory, from #890, ran out of memory in a fetch and fetched the same row again. A fetch whose conversion fails now completes the result, so that row is never read again, and the test lost its point. A fetch of zero rows looks at the next row without reading it, and running out of memory there leaves the result going on, so the test now runs out of memory there and reads the row again in the next fetch, as before. A new test pins what a fetch that runs out of memory does now: it raises the error once, completes the result, and further fetches return zero rows without stepping the statement again. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd8MVXyvU7qoMeruD1R
When SQLite runs out of memory while it converts a column value, for instance while transcoding a large text value from a UTF-16 database under a heap limit,
sqlite3_column_text()andsqlite3_column_blob()returnNULLand setSQLITE_NOMEMon the connection. Withcheck_utf8 = FALSE(the default, and the only behavior of RSQLite 3.53.3 from CRAN),fetch_string()passed thatNULLtoRf_mkCharCE()and R crashed. Withcheck_utf8 = TRUE,fetch_checked_string()returned""and relied on the nextsqlite3_step()failing in the same fetch; with the read-ahead of #850, that step runs ahead and keeps its error, so the""would reach the caller. Theextended_typesdate, datetime and time readers passed theNULLon to their parsers.What changes
SqliteColumnDataSourcegetscheck_out_of_memory(), which raisesCannot read column <j>: out of memory(SQLite's own message) when a text or blob pointer isNULLandsqlite3_errcode()isSQLITE_NOMEM, as the SQLite documentation describes for these functions. ANULLfor a NULL value, or aNULLblob withsqlite3_column_bytes() == 0(an empty blob), is never an error: the connection can still holdSQLITE_NOMEMfrom an earlier read of the same row, and such a value reads as"", as before.fetch_text()wrapssqlite3_column_text()with that check;fetch_string()(withcheck_utf8 = FALSE),fetch_date(),fetch_datetime_local()andfetch_time()use it.fetch_checked_string()runs the check on both its text and its blob pointer, so it no longer depends on the following step, and its comment no longer claims that the""never reaches the caller.fetch_blob()already raised an error for aNULLblob with a nonzero size and is unchanged.src/rsqlite.cpp,sqlite_heap_limits()andsqlite_memory_used(), let the tests cap SQLite's heap and lift the cap afterwards.PRAGMA hard_heap_limitcan only lower the limit, and lowering the hard limit also lowers the soft limit for the whole process; leaving the soft limit behind madetest-transactions.Rfail with "database is locked" in an earlier draft, so the helper restores both.Left out
Other SQLite accessors that return
NULLon out of memory, such assqlite3_column_name()andsqlite3_column_decltype(), are not part of this change;sqlite3_column_decltype()returningNULLis already handled as "no declared type".Tests
tests/testthat/test-out-of-memory.R, with fixtures inhelper-heap-limit.R, caps the heap at 16 MB above SQLite's current use and reads a 4-million-character value from an in-memory UTF-16 database: bothcheck_utf8settings and aDATEcolumn withextended_types = TRUEnow raise "Cannot read column 2: out of memory". Measured on Linux, the error appears for headroom from about 8.5 MB (below that the step itself fails with "out of memory") up to 24 MB, and the value reads in full from 26 MB on, so 16 MB sits in the middle; because the sizes depend on SQLite's allocator, these two blocks are skipped on CRAN. A third block reads the same row again after the error and expects the step's "out of memory", not an error blamed on an empty blob in an earlier column. Two further blocks, which also pass onmain, read the value in full without a cap and read empty text,X''andzeroblob(0)as""in a UTF-16 database with bothcheck_utf8settings.devtools::test()withNOT_CRAN=true: no failures besides the three intest-httpvfs.Rthatmainshows too (#832, #837), andtest-dbConnect.R's busy-handler test, which needs an installed RSQLite in itscallrchild and has none in this environment.The review's reproducer (heap limit of 16 MB, 4 MB text in a UTF-16 file database) under valgrind 3.22.0: on
mainwithcheck_utf8 = FALSE, "Invalid read of size 1" and a segfault; onmainwithcheck_utf8 = TRUE, "out of memory" from the next step; on this branch, "Cannot read column 2: out of memory" for both settings and 0 valgrind errors. The new test file under valgrind also reports 0 errors and no definite leaks. RSQLite 3.53.3 from CRAN segfaults on the same experiment at limits of 10 to 24 MB.Place in the stack
Based on
main(3f746a0) and independent of the open stack. Once #850 lands on top,check_utf8 = TRUEraises the error in the fetch that reads the value rather than returning""and deferring the error to the next fetch.Review and accepted limitations: Accepted limitations; the pre-existing
sqliteCopyDatabase()crash found in review is #891.Closes #889.
Coordinating session: https://claude.ai/code/session_01Hr1rd7mthffYErGmWTGDfQ
Generated by Claude Code