Skip to content

fix(sqlite): don't panic building URL for in-memory connect options#4329

Open
chatman-media wants to merge 1 commit into
transact-rs:mainfrom
chatman-media:fix/sqlite-in-memory-url-panic
Open

fix(sqlite): don't panic building URL for in-memory connect options#4329
chatman-media wants to merge 1 commit into
transact-rs:mainfrom
chatman-media:fix/sqlite-in-memory-url-panic

Conversation

@chatman-media

Copy link
Copy Markdown

SqliteConnectOptions::to_url_lossy() panics for in-memory databases. The filename for an in-memory DB is either :memory: (the default) or the generated file:sqlx-in-memory-N, and build_url()'s percent-encode set doesn't touch :, so it ends up embedding a raw colon right after sqlite://. The url crate then reads that as a host:port authority instead of a path and dies with InvalidPort (or EmptyHost for the plain :memory: case).

sqlx::sqlite::SqliteConnectOptions::from_str("sqlite::memory:")?.to_url_lossy(); // panics

Fixed by adding : to the encode set used when building the URL — it's not part of the WHATWG path-percent-encode-set this was modeled on, but it needs to go since it's ambiguous with authority syntax right after the scheme. Doesn't affect normal file paths since those don't have a leading colon problem. Added a test that hits both the :memory: default and the file:sqlx-in-memory-N path, confirmed it panics on main and passes with the fix.

Closes #4327

build_url() percent-encodes the filename with a set that leaves ':' alone,
so in-memory options (filename `:memory:` or the generated
`file:sqlx-in-memory-N`) produce something like `sqlite://file:sqlx-in-memory-0`.
The url crate reads that as a host:port authority and blows up with
InvalidPort/EmptyHost instead of returning a URL. Encoding ':' too fixes it
without touching how normal file paths get encoded.
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.

SQLite Connect Options for Memory DB Panics when Building URL

1 participant