Repository navigation
Sqlite.close: no second sqlite3_close on freed memory - #61
Merged
Merged
Conversation
`close` called `sqlite3_close(db)` and left the pointer in the external
object, so the finalizer closed it again later, and any call after
`close` used the freed connection. The comment said this was fine
because sqlite3_close is idempotent; it is not.
The external object now holds a small box `{ sqlite3 *db; }`. `close`
closes the connection and sets `db` to NULL in the box, so every
reference to the `Db` sees it closed: `exec`, `execp` and `query` fail
with "database is closed", a second `close` does nothing, and the
finalizer closes the connection only if it is still open, then frees
the box. (`lean_set_external_data` could not do this: on a shared
object it returns a new one and the other references keep the old
pointer.) Statements are prepared and finalized within one call, so
none outlives the connection.
persist_spec has a `Sqlite.close` group: close twice, exec / execp /
query after close, the same through a `Backend` holding another
reference, and 100 connections left to the finalizer, closed or not.
Under valgrind the old code reports invalid reads in sqlite3_close and
sqlite3_prepare_v2; with this change it reports 0 errors.
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.
Sqlite.closecalledsqlite3_close(db)and left the pointer in the external object. The finalizer then closed it a second time, and any call aftercloseused the freed connection. The comment inclosesaid this was safe becausesqlite3_closeis idempotent. It is not.Change
{ sqlite3 *db; }.closecloses the connection and setsdbto NULL, so every reference to theDbsees it closed:exec,execpandqueryfail with…: database is closed;closedoes nothing;lean_set_external_datacan't do this: on a shared object it returns a new object, and the other references keep the old pointer. The new comment says so.Dbandclosedescribe the new behaviour.Tests
persist_spec has a new
Sqlite.closegroup (8 assertions):exec/execp/queryafter close;Backendthat holds another reference;With the old
leantea_sqlite.cand the new spec, valgrind reports invalid reads insqlite3_close(second close) andsqlite3_prepare_v2(use after close) on blocks freed by the firstsqlite3_close.I didn't try ASan: the static library is linked by Lean's bundled toolchain, which has no sanitizer runtime.
Not covered
A
closeracing with a call on another thread is still a data race on the box. Before this change that case was a use-after-free.