Skip to content

fix: Name the parameter and the row of Arrow values whose offsets point outside the data - #971

Draft
krlmlr wants to merge 5 commits into
claude/rb-arrow-bind-typesfrom
claude/rb-arrow-param-checks
Draft

krlmlr wants to merge 5 commits into
claude/rb-arrow-bind-typesfrom
claude/rb-arrow-param-checks

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior for users: this improves the errors of the internal bind_arrow_stream(), which no DBI method calls yet.

What changes

Offsets outside the data name their parameter and row. #967 validates every array of the stream at nanoarrow's full level, which checks every offset of utf8 and binary values. Its message names neither the parameter nor the row, and counts within the array from 0: "Invalid Arrow parameters: [3] Expected element size >= 0". When the full validation of an array fails, SqliteArrowParamSource::check_offsets() now looks for the first row with a utf8, large utf8, binary or large binary value, also as the value of a dictionary, whose offsets are negative, decreasing or beyond the data. It fails like a value that can't be bound, with the parameter, its name and the row counted across the stream: "Can't bind parameter 1 ($x) in row 2: the offsets of its value point outside the data." This is the counterpart of #970's check_dictionary_indexes() for offsets, but it runs only after the validation has failed, so a valid array costs nothing more. An array that is invalid only where no row reads it, such as under a null struct row, still fails with nanoarrow's message, and so does a negative first offset, which ArrowArrayViewSetArray() refuses before the full validation runs. Since the last offset gives the size of the data, only an offset before the last one can lie beyond the data. As before, the error comes when the array is read, before any of its rows runs.

Text is checked before it is bound. #967 scanned text for NUL bytes and UTF-8 before binding it, also text beyond SQLite's length limit. Text whose length exceeds the connection's current limit (sqlite3_limit(conn, SQLITE_LIMIT_LENGTH, -1)) now fails with "string or blob too big" (RSQLite_error_toobig) without its data being read, as blobs already did through sqlite3_bind_blob64(). Then text with a NUL byte or invalid UTF-8 fails as before, and only valid text is bound, so invalid text is never copied into SQLite. A large utf8 value that claims more bytes than its buffer holds was scanned out of bounds; it now fails unread, but only if the claimed length exceeds the limit. The Arrow C data interface carries no buffer sizes, so a claimed length within the limit over a shorter buffer can't be detected.

What of #867 was already covered

Tests

The new tests/testthat/test-arrow-bind-checks.R, with helpers in helper-arrow.R that build struct arrays, offsets and dictionaries without validation: offsets beyond the data and decreasing at the second row for all four string and binary types and for the value a dictionary index points to; nanoarrow's message for bad offsets under a null struct row; with the length limit lowered to 30 bytes (SQLite raises any lower limit to 30), 31-byte values of utf8, large utf8, binary, large binary, a factor, and text with a NUL byte or invalid UTF-8 fail at their row with RSQLite_error_toobig after the first row ran, while 30-byte text with a NUL byte or invalid UTF-8 fails with its own message; at the default limit, which src/Makevars sets through SQLITE_MAX_LENGTH to 2147483647 bytes, large utf8 and large binary values that claim 3e9 bytes over a one-byte buffer fail with RSQLite_error_toobig. One expectation in test-arrow-bind.R now expects the new offsets message. On the base, the new tests fail with nanoarrow's message for the offsets and with "the text contains a NUL byte" for text beyond the limit (red); they pass with this change (green). Every test_that() block passes on its own after load_all(). valgrind (--leak-check=full) reports no errors and nothing lost for test-arrow-bind-checks.R. The full suite with NOT_CRAN=true: FAIL 0, WARN 3 (the known test-encoding.R warnings), SKIP 64, PASS 13451.

Layer F9 of the rebuilt chain, planned in #880; based on #970. Ported from #867, minus what #967 and #970 already cover.

Coordinating session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

@krlmlr
krlmlr added this pull request to stack #928 December 1, 2026 10:35
…nt outside the data

The full validation of each array refuses offsets of a utf8 or binary value
that are negative, decreasing or beyond the data,
with a message that names neither the parameter nor the row
and counts within the array from 0.
When it fails, the rows of the array are now checked for such offsets,
also of the value that a dictionary index points to,
and the first one fails like a value that can't be bound,
with the parameter, its name and the row counted across the stream.
The check runs only after the validation has failed,
so a valid array costs nothing more.

Text is now bound before it is checked for NUL bytes and UTF-8,
so that a value beyond SQLite's length limit fails with SQLITE_TOOBIG
before its data is read, as blobs already did.

Ported from #867.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
@krlmlr krlmlr changed the title fix: Name the parameter and the row of invalid Arrow parameter values fix: Name the parameter and the row of Arrow values whose offsets point outside the data Dec 1, 2026
@krlmlr
krlmlr force-pushed the claude/rb-arrow-param-checks branch from bab0da7 to fb7666d Compare December 1, 2026 10:39
As `arrow_int_array()` does since #970,
so that the tests also hold on big-endian machines.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…efore binding it

Binding first copied invalid text into SQLite before refusing it,
and a huge invalid value could fail for lack of memory.
Text beyond the connection's length limit now fails as SQLITE_TOOBIG unread,
then text with a NUL byte or invalid UTF-8 fails,
and only valid text is bound.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants