diff --git a/src/lib.rs b/src/lib.rs index bb9382b..c4860d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -265,6 +265,13 @@ fn len_as_c_int(len: usize) -> Result { } } +#[cfg(unix)] +fn path_to_cstring(p: &Path) -> Result { + use std::os::unix::ffi::OsStrExt; + Ok(CString::new(p.as_os_str().as_bytes())?) +} + +#[cfg(not(unix))] fn path_to_cstring(p: &Path) -> Result { let s = p.to_str().ok_or_else(|| Error::InvalidPath(p.to_owned()))?; str_to_cstring(s) @@ -1019,6 +1026,35 @@ mod test { } } + #[cfg(unix)] + #[test] + fn test_invalid_unicode_file_names() { + use std::ffi::OsStr; + use std::fs::File; + use std::os::unix::ffi::OsStrExt; + let temp_dir = tempfile::tempdir().unwrap(); + + let path = temp_dir.path(); + if File::create(path.join(OsStr::from_bytes(&[0xFE]))).is_err() { + // Skip test, filesystem doesn't support invalid Unicode + return; + } + let db_path = path.join(OsStr::from_bytes(&[0xFF])); + { + let db = Connection::open(&db_path).unwrap(); + let sql = "BEGIN; + CREATE TABLE foo(x INTEGER); + INSERT INTO foo VALUES(42); + END;"; + db.execute_batch(sql).unwrap(); + } + + let db = Connection::open(&db_path).unwrap(); + let the_answer: Result = db.query_row("SELECT x FROM foo", NO_PARAMS, |r| r.get(0)); + + assert_eq!(42i64, the_answer.unwrap()); + } + #[test] fn test_close_retry() { let db = checked_memory_handle();