Skip to content

fix: Internal result entry points refuse a pointer that is not a result's - #937

Draft
krlmlr wants to merge 4 commits into
claude/rb-failed-bind-statefrom
claude/rb-result-tag
Draft

krlmlr wants to merge 4 commits into
claude/rb-failed-bind-statefrom
claude/rb-result-tag

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior for code that uses the DBI methods. The internal entry points that take a result, such as result_fetch(), result_has_completed(), result_get_placeholder_names(), result_valid() and result_release(), now fail with "Invalid result set" when given an external pointer that is not a result's, such as a connection's, instead of reading the connection's memory as a result's. Only code that calls them with ::: can get there, so there is no NEWS bullet.

How: result_create() sets the tag of the result's external pointer to the symbol RSQLite_result. The as_cpp() conversions to DbResult* and SqliteResult* in src/RSQLite_types.h, result_release() and result_valid() check that tag first; the conversions then keep failing for a NULL address as before. The SqliteResult* conversion now goes through the DbResult* one with a static_cast instead of casting the address directly. A result saved with saveRDS() and restored keeps its tag and holds NULL, so it still fails with "Invalid result set" and dbIsValid() returns FALSE. result_valid() and result_release() first treat an external pointer that holds NULL as an expired result, tagged or not, so dbIsValid() still returns FALSE and dbClearResult() still returns TRUE with the "Expired" warning for a result made with new("SQLiteResult") or saved by an RSQLite that didn't tag its results. A non-pointer argument fails with "Invalid result set" in the DbResult* and SqliteResult* conversions; result_valid() and result_release() reject it earlier, in cpp11's own conversion.

Connections are not tagged here. Their entry points take cpp11::external_pointer<DbConnectionPtr> directly rather than through one conversion, so a check would be a line in each of about ten functions in src/connection.cpp, src/extensions.cpp and result_create(), and a new message would have to be chosen; a result's pointer passed to them still reads the result as a connection. That seems better as a separate change if wanted.

Tests: a new block in tests/testthat/test-dbClearResult.R passes a connection's pointer to result_fetch(), result_has_completed(), result_get_placeholder_names(), result_valid() and result_release(), and checks a saved and restored result; a second block checks dbIsValid() and dbClearResult() on new("SQLiteResult") and on an untagged pointer that went through serialize() and unserialize(), which errored before the follow-up fix. Red on the base (each call in its own callr subprocess, installed from claude/rb-failed-bind-state): result_has_completed(), result_rows_fetched() and result_get_placeholder_names() crash R; result_fetch(), result_column_info() and result_release() happen to read the busy flag as set and fail with the busy-guard message, and result_valid() returns TRUE. The issue's reproducer with result_fetch() thus no longer crashes on this base, but by chance. Green with the fix: all of them fail with "Invalid result set", in process. Full suite with NOT_CRAN=true: FAIL 0, WARN 3 (locale warnings from test-encoding.R), SKIP 63, PASS 11000.

Layer B6 of the rebuilt chain, planned in #880; based on #936. Not ported from an old PR: the issue was found by the review of #859.

Closes #876.

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



Generated by Claude Code

…lt's

`result_create()` tags the external pointer of a result with the symbol `RSQLite_result`.
The conversions to `DbResult*` and `SqliteResult*`, `result_release()` and `result_valid()`
fail with "Invalid result set" for a pointer without that tag, such as a connection's.
Before, they read the connection's memory as a result's, and some crashed R.
A result saved and restored keeps the tag and still fails with "Invalid result set".

Closes #876.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
@krlmlr
krlmlr added this pull request to stack #928 December 1, 2026 00:45
`dbIsValid()` returns `FALSE` again
and `dbClearResult()` returns `TRUE` with the "Expired" warning
for a result made with `new()`
or saved by an RSQLite that didn't tag its results.
Other untagged pointers still fail with "Invalid result set".

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.

Internal result entry points crash when given a connection's pointer

2 participants