Skip to content

sqlite: lossily coerce invalid UTF-8 in custom collation callback#4219

Merged
abonander merged 1 commit intolaunchbadge:mainfrom
joaquinhuigomez:fix/sqlite-collation-invalid-utf8
Apr 7, 2026
Merged

sqlite: lossily coerce invalid UTF-8 in custom collation callback#4219
abonander merged 1 commit intolaunchbadge:mainfrom
joaquinhuigomez:fix/sqlite-collation-invalid-utf8

Conversation

@joaquinhuigomez
Copy link
Copy Markdown
Contributor

The safe Fn(&str, &str) -> Ordering signature exposed by SqliteConnectOptions::collation() and LockedSqliteHandle::create_collation() was backed by from_utf8_unchecked, so a database containing invalid UTF-8 text could reach the user callback and materialize &str values that violate Rust's UTF-8 invariant inside a safe API.

SQLite explicitly documents that invalid UTF-8 may be passed into application-defined collating sequences, so the FFI shim must not assume well-formed bytes. This patch replaces from_utf8_unchecked with String::from_utf8_lossy, which keeps the safe signature sound without changing behavior for valid UTF-8 input.

Fixes #4194

The safe Fn(&str, &str) -> Ordering signature exposed by
SqliteConnectOptions::collation() and LockedSqliteHandle::create_collation()
was backed by from_utf8_unchecked, so a database containing invalid UTF-8
text could reach the user callback and materialize &str values that violate
Rust's UTF-8 invariant inside a safe API.

SQLite explicitly documents that invalid UTF-8 may be passed into
application-defined collating sequences, so the FFI shim must not assume
well-formed bytes. Replace from_utf8_unchecked with String::from_utf8_lossy,
which matches the sqlite3_create_collation_v2 SQLITE_UTF8 flag and keeps
the safe signature sound without changing correct-UTF-8 behavior.

Fixes launchbadge#4194
@abonander abonander merged commit e72f822 into launchbadge:main Apr 7, 2026
146 checks passed
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: safe custom collation APIs can pass invalid UTF-8 as &str

2 participants