From 8ea257afe9534e58cf90c5ee8ccad4fc7545d416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20CORTIER?= Date: Thu, 27 Feb 2025 20:18:38 +0900 Subject: [PATCH] fix(libsql-sys): support non-Unix when rusqlite feature is disabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the codepath used when rusqlite is disabled, is Unix-platform-dependent. This patch is adding a platform-independent branch. The database path is expected to be UTF-8 by the libsql native library. However, in the real world, all Unix paths are not necessarily UTF-8. For this reason, I understand why the Unix codepath is attempting a direct conversion from OsString to CString. However, it’s not possible to do something similar on other platforms. For these platforms, we are enforcing correct UTF-8 using to_str. At least, it should work reasonably well in most cases. --- libsql-sys/src/connection.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/libsql-sys/src/connection.rs b/libsql-sys/src/connection.rs index 7182cbbace..f3d071bd4b 100644 --- a/libsql-sys/src/connection.rs +++ b/libsql-sys/src/connection.rs @@ -273,9 +273,27 @@ impl Connection { #[cfg(not(feature = "rusqlite"))] let conn = unsafe { - use std::os::unix::ffi::OsStrExt; - let path = std::ffi::CString::new(path.as_ref().as_os_str().as_bytes()) - .map_err(|_| crate::error::Error::Bug("invalid database path"))?; + #[cfg(unix)] + let path = { + use std::os::unix::ffi::OsStrExt; + std::ffi::CString::new(path.as_ref().as_os_str().as_bytes()).map_err(|_| { + crate::error::Error::Bug( + "invalid database path containing an internal nul byte", + ) + })? + }; + #[cfg(not(unix))] + let path = path + .to_str() + .ok_or_else(|| crate::error::Error::Bug("database path is not valid unicode")) + .and_then(|x| { + std::ffi::CString::new(x).map_err(|_| { + crate::error::Error::Bug( + "invalid database path containing an internal nul byte", + ) + }) + })?; + let mut conn: *mut crate::ffi::sqlite3 = std::ptr::null_mut(); // We pass a pointer to the WAL methods data to the database connection. This means // that the reference must outlive the connection. This is guaranteed by the marker in