Skip to content

fix: Refuse to clear a result while one of its fetches or binds is running - #611

Closed
krlmlr wants to merge 2 commits into
mainfrom
claude/fetch-clear-uaf
Closed

krlmlr wants to merge 2 commits into
mainfrom
claude/fetch-clear-uaf

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Closes #601.

Before this change, a warning that dbFetch() raised ran R's calling handlers while the C++ fetch was still on the stack. A handler that called dbClearResult() deleted the result, and the fetch then read freed memory. Valgrind reported invalid reads, and the session could crash.

Change

  • DbResult::fetch() and DbResult::bind() now run through run_guarded(), ported from RSQLite (fix: Never restart a statement after an error in dbFetch() or dbBind() RSQLite#830). It sets a busy flag and uses R_ExecWithCleanup(), so the flag is reset even when the call ends with an R error.
  • DbResult::busy() exposes the flag. While it is set, dbClearResult() fails with "Can't clear a result while one of its fetches or binds is running, as from R code that it calls, such as a condition handler.". A nested dbFetch() or dbBind() from such R code is refused with a similar message.
  • This covers prepared results, immediate = TRUE results (which raise the same warning), and any R callback during a bind.
  • src/MariaRow.* and src/MariaResultPrep.* are not touched, since feat: ALTREP-backed dbFetch() with storage-aware column types #587 rewrites them.

Tests

tests/testthat/test-dbClearResult.R (new) checks the error messages for dbClearResult() and for a nested dbFetch() and dbBind(), that the fetch returns an empty result, and that the result stays valid and can be cleared afterwards. Under valgrind, the issue's reproducer gives invalid reads and then a segfault on main, and 0 errors with this change, for both the prepared and the immediate = TRUE path. Local run against MariaDB 10.11 with NOT_CRAN=true: the whole suite had 0 failures and the one test-timezone.R error ("US/Alaska" unknown to CCTZ in this container), which also happens on main. MySQL was not tested: there is no server in this container, and CI's MySQL entries currently run MariaDB (#592).

Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ


Generated by Claude Code

…onger frees the result in use

`dbFetch()` on a statement raises a warning from C++,
and a calling handler that cleared the result freed the object that the fetch still used.
A result now counts as busy while one of its fetches or binds runs,
and `dbClearResult()` refuses a busy result with an error,
so the handler sees the error and the fetch returns its empty data frame.

Closes #601.

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

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

The clean review found nothing above Low in this change. Its three Lows are fixed in c2d9b13: the messages now name any R callback rather than only warning handlers, DbResult::busy() replaces check_not_busy(), and new tests cover a nested dbFetch() and dbBind() from a handler.


Generated by Claude Code

@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Rethink an alternative: queue and raise the warning just before returning to R.

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Working on it, as an alternative PR from main, so that the two can be compared before one is closed. Warnings raised during a fetch, a bind or the setup of a result go into a queue, and the entry points in src/result.cpp and src/connection.cpp raise them after the C++ code is done with the result. No R code then runs while C++ holds result state, so dbClearResult() from a warning handler becomes safe instead of being refused. Destructors won't call into R at all.

The same idea may also cover the interrupt check in the fetch loop (#621, fixed in #629 with the busy flag from this PR): detect a pending interrupt without running handlers, stop the fetch cleanly, and let R handle the interrupt after returning. That is being investigated. If it works, #611, the busy-check part of #624 and #629 could all be replaced; I'll say which ones once the alternative has been reviewed.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

The alternative is up as draft #632: warnings are queued and raised after the C++ work, so a warning handler can call dbClearResult() safely. #624 is being rebuilt on main without this PR, and #629 without the busy flag. If #632 suits you after its clean review, this PR can be closed in its favor.


Generated by Claude Code

@krlmlr
krlmlr removed this pull request from stack #625 November 29, 2026 14:08
@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

The alternative is preferred, closing.

@krlmlr krlmlr closed this Nov 29, 2026
krlmlr added a commit that referenced this pull request Nov 29, 2026
…632)

* fix: Raise warnings only after the C++ code is done with the result

Closes #601.

Warnings raised while C++ code holds a result or a connection are queued
and signaled by the entry points just before returning to R,
so that a calling handler can no longer free the result or close the connection under our feet.
The warning about cancelling the previous query is raised before the new result is created,
and destructors only queue.
This is an alternative to #611, which refuses dbClearResult() from such a handler.

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

* chore: Auto-update from GitHub Actions

Run: https://github.com/r-dbi/RMariaDB/actions/runs/46270935930

* fix: Raise warnings from destructors at top level, not in an unrelated call

The warning for a connection collected by gc() went into the same queue
as the warnings of the current call,
and surfaced in the next call on any connection.
With options(warn = 2), it turned a healthy call into an error and lost its result.

Destructors now queue into a separate queue.
A task callback registered in .onLoad() drains it after each top-level call,
and R prints its warnings after the output of that call.
An error from the callback, e.g. with options(warn = 2), is printed
and keeps the callback registered.

The tests ignore unrelated warnings.

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

* fix: Drop warnings left by a failed call at the start of the next one

A call that queued a warning and then failed never signaled it,
so the warning surfaced in a later, unrelated call.
Each entry point that signals queued warnings now clears the queue first.
A warning that preceded an error is dropped, the error is what matters.

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

* fix: Release a new result when raising its warnings fails

With options(warn = 2) or an exiting warning handler,
dbSendQuery() never returned the new result,
which stayed the connection's current result until garbage collection.

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

* fix: Register the warning callback by name, and raise leak warnings in `dbConnect()` too

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

* refactor: Raise leak warnings at the start of the next call instead of from a task callback

Drop the top-level task callback.
Warnings queued by destructors during garbage collection are now raised
at the start of the next call into the library
(result_create(), result_fetch(), result_bind(), connection_create()),
before any C++ work, so that an error under options(warn = 2)
leaves no state changed and no result lost.
The leak warning text says that the connection may stem from an earlier operation.

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

* fix: Keep the destructor from throwing, and restore a signature's formatting

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

---------

Co-authored-by: Claude <[email protected]>
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.

Use-after-free in dbFetch() when a warning handler clears the result

2 participants