From 22fe783df7cc1acd12004fd9bc795d0a9044e89d Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Sun, 31 Jan 2016 12:04:39 -0700 Subject: [PATCH] Allow nul bytes in strings w/ SQLite Unlike PG, SQLite actually allows all valid UTF-8 strings in its data type which is documented to be UTF-8. I find this especially ironic, since PG does not interface over C, but basically follows C semantics, while SQLite which we *only* access via C, does not. --- diesel/src/connection/sqlite/sqlite_value.rs | 7 ++++--- diesel/src/types/impls/sqlite.rs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/diesel/src/connection/sqlite/sqlite_value.rs b/diesel/src/connection/sqlite/sqlite_value.rs index 75bd6a46f4bc..267e4cb43a7e 100644 --- a/diesel/src/connection/sqlite/sqlite_value.rs +++ b/diesel/src/connection/sqlite/sqlite_value.rs @@ -1,7 +1,6 @@ extern crate libsqlite3_sys as ffi; extern crate libc; -use std::ffi::CStr; use std::{slice, str}; use backend::Sqlite; @@ -25,10 +24,12 @@ impl SqliteValue { } } - pub fn read_text(&self) -> Result<&str, str::Utf8Error> { + pub fn read_text(&self) -> &str { unsafe { let ptr = ffi::sqlite3_column_text(self.inner_statement, self.col_index); - CStr::from_ptr(ptr as *const libc::c_char).to_str() + let len = ffi::sqlite3_column_bytes(self.inner_statement, self.col_index); + let bytes = slice::from_raw_parts(ptr as *const u8, len as usize); + str::from_utf8_unchecked(bytes) } } diff --git a/diesel/src/types/impls/sqlite.rs b/diesel/src/types/impls/sqlite.rs index 16df05dee4c7..e063217356f2 100644 --- a/diesel/src/types/impls/sqlite.rs +++ b/diesel/src/types/impls/sqlite.rs @@ -7,7 +7,7 @@ use types::{self, FromSql}; impl FromSql for String { fn from_sql(value: Option<&SqliteValue>) -> Result> { - let text = try!(not_none!(value).read_text()); + let text = not_none!(value).read_text(); Ok(text.into()) } }