Repository navigation
fix: Never use query text or SQLite messages as a printf() format, and guard fetch_blob() against NULL - #869
Conversation
`cpp11::warning()` and `cpp11::stop()` treat their first argument as a format. The warning for the ignored rest of a multi-statement query, the error raised from `sqlite3_errmsg()`, and the mixed-type column warning passed user-controlled text there, so a `%n` in a query, a table name or a column name aborted R and other conversions printed garbage. These calls now pass a fixed format with the text as an argument. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
`sqlite3_column_blob()` returns `NULL` for an empty blob, and passing it to `memcpy()` is undefined behavior even for zero bytes. An empty blob now returns `raw(0)` directly, and a `NULL` pointer with a nonzero size, which means that SQLite ran out of memory, raises an error instead of crashing. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
|
Review of this pull request at 27a8236, by a Claude Code reviewer with clean context that read the description, the two commits and the diff against What was run
What holds
HighNone. MediumNone. Low
Generated by Claude Code |
`params.size()` is an `R_xlen_t`, which does not match the `%i` in "Query requires %i params; %i supplied.". Also note in `fetch_blob()` that `sqlite3_column_bytes()` must stay before `sqlite3_column_blob()`, since the out-of-memory check relies on it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
Accepted limitationsThe clean review of 27a8236 found nothing above Low: each new format-string test aborts R on
Generated by Claude Code |
Bring in #853 (gcc continuation lines in the generated `.dd` files) and #869 (no query text or SQLite message as a `printf()` format, `fetch_blob()` guarded against `NULL`). The conflict in `SqliteResultImpl::raise_sqlite_exception()` keeps this branch's `throw std::runtime_error(sqlite3_errmsg(conn))`: a failed prepare reaches R at cpp11's entry point like every other step error here, and cpp11 passes the message through `"%s"`, so it is not a format string either. The `.dd` files regenerated with the new `src/Makevars.deps` are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
Decided in #829 (comment): a small separate pull request from
main, independent of the Arrow stack.What changes and why
cpp11::warning()andcpp11::stop()treat their first argument as aprintf()format unlessCPP11_USE_FMTis defined. Three calls passed user-controlled text there, so a%nin a query, a table name or a column name aborted R with*** %n in writable segment detected ***, and other conversions such as%d%dprinted garbage:SqliteResultImpl::prepare(): the "Ignoring remaining part of query: " warning appended the rest of a multi-statement query (dbGetQuery(con, "SELECT 1; SELECT '%n'")).SqliteResultImpl::raise_sqlite_exception(): passedsqlite3_errmsg(), which quotes table and column names (dbGetQuery(con, 'SELECT * FROM "x%n"')).DbColumn::warn_type_conflicts(): the mixed-type warning includes the column name. This is the column-name variant found in the review of fix: Validate blobs that are read into a text column #799; that reimplementation no longer carries the fix, so it lands here.All three now pass a fixed
"%s"-style format with the text as an argument. A remaining query that merely contains a literal%, such asSELECT 1; SELECT 'café 100%', gave an empty warning onmain(confirmed by the review); the warning now shows the remaining query as written. The "Query requires %i params; %i supplied." error passedparams.size(), anR_xlen_t, for%i; it is now cast toint. Every othercpp11::stop(),cpp11::warning(),Rf_error()andRf_warning()call insrc/(outside the vendored SQLite) already uses a literal format.SqliteColumnDataSource::fetch_blob()passed the pointer fromsqlite3_column_blob()tomemcpy()unchecked. For an empty blob (SELECT X'' AS x) that pointer isNULL, which is undefined behavior even for zero bytes (UBSan reports "null pointer passed as argument 2"). An empty blob now returnsraw(0)withoutmemcpy(), and aNULLpointer with a nonzero size, which means SQLite ran out of memory, raises an error instead of crashing. A comment notes thatsqlite3_column_bytes()must stay beforesqlite3_column_blob(), since that check relies on the order.Left out
raise_sqlite_exception()is replaced by astd::runtime_errorin #795 of the Arrow stack; this pull request fixesmainnow and does not anticipate that change. With the bundled SQLite,zeroblob()is materialized when the statement is stepped, so an out-of-memoryzeroblob()already fails with "out of memory" fromdbSendQuery(); the newNULLcheck infetch_blob()is a guard for the case where the expansion happens on access, and has no test because it could not be triggered here.Tests
tests/testthat/test-format-strings.R: the multi-statement warning with%n,%d%dand%s%s%s; "no such table" errors with the same patterns; a table and a column named with%n%s, read back unchanged, with the "already exists", "no such column" and mixed-type messages intact. These abort R onmain.tests/testthat/test-blob.R: empty blobs fromX''andzeroblob(0), next to a non-empty one, and an empty blob round-tripped throughdbWriteTable()anddbReadTable().-fsanitize=undefined) without reports; the same build of the oldfetch_blob()reports theNULLargument tomemcpy().devtools::test(): all pass apart from the threetest-httpvfs.Rerrors that also happen onmain(test: Run the HTTP VFS tests for real, and make their errors fail the check #832, testthat hides a test's error when the test records another result after it #837) and one "interrupt in busy handler" error intest-dbConnect.Rthat needs RSQLite installed in the library its callr subprocess uses.https://claude.ai/code/session_01Hr1rd432Aef98ibnZSSsYn
Generated by Claude Code