Repository navigation
{180274222}: Don't let a temp btree become the thread's table btree - #6413
Merged
Merged
Conversation
sqlite3BtreeCursor() set thd->bt to whatever btree the thread opened its first cursor on, temporary ones included. When that temp btree was closed, sqlite3BtreeClose() only cleared thd->bttmp (if/else-if), so thd->bt was left pointing at freed memory for the life of the SQL thread. Anything walking thd->bt->cursors then missed the real table cursors. In a chunked transaction, chunk_transaction() failed to clear cur->shadtbl before the chunk commit freed the shadow tables, and the next insert dereferenced the freed shadow table in osql_save_insrec(). It reproduces with a query whose first cursor is on a temp table (e.g. "select 1 union select 2") on a fresh SQL thread, followed by a chunked INSERT ... SELECT spanning more than one chunk. Only set thd->bt for non-temporary btrees, and clear thd->bt and thd->bttmp independently on close. Signed-off-by: Salil Chandra <schandra107@bloomberg.net>
roborivers
approved these changes
Oct 27, 2026
roborivers
left a comment
There was a problem hiding this comment.
Cbuild submission: Success ✓.
Regression testing: Success ✓.
markhannum
approved these changes
Oct 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
sqlite3BtreeCursor() set thd->bt to whatever btree the thread opened its first cursor on, temporary ones included. When that temp btree was closed, sqlite3BtreeClose() only cleared thd->bttmp (if/else-if), so thd->bt was left pointing at freed memory for the life of the SQL thread.
Anything walking thd->bt->cursors then missed the real table cursors. In a chunked transaction, chunk_transaction() failed to clear cur->shadtbl before the chunk commit freed the shadow tables, and the next insert dereferenced the freed shadow table in osql_save_insrec(). It reproduces with a query whose first cursor is on a temp table (e.g. "select 1 union select 2") on a fresh SQL thread, followed by a chunked INSERT ... SELECT spanning more than one chunk.
Only set thd->bt for non-temporary btrees, and clear thd->bt and thd->bttmp independently on close.