From ac30e169ae51b262bc8cf7026469851ce39b23c6 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Tue, 14 Apr 2020 02:04:19 -0700 Subject: [PATCH] Use SmallCString in most places --- src/inner_connection.rs | 13 +++++++------ src/lib.rs | 7 ++++--- src/session.rs | 8 +++++--- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/inner_connection.rs b/src/inner_connection.rs index 2b5f4bb..95b2d82 100644 --- a/src/inner_connection.rs +++ b/src/inner_connection.rs @@ -1,4 +1,4 @@ -use std::ffi::CString; +use std::ffi::CStr; use std::os::raw::{c_char, c_int}; #[cfg(feature = "load_extension")] use std::path::Path; @@ -8,7 +8,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use super::ffi; -use super::{str_for_sqlite, str_to_cstring}; +use super::str_for_sqlite; use super::{Connection, InterruptHandle, OpenFlags, Result}; use crate::error::{error_from_handle, error_from_sqlite_code, Error}; use crate::raw_statement::RawStatement; @@ -51,9 +51,9 @@ impl InnerConnection { } pub fn open_with_flags( - c_path: &CString, + c_path: &CStr, flags: OpenFlags, - vfs: Option<&CString>, + vfs: Option<&CStr>, ) -> Result { #[cfg(not(feature = "bundled"))] ensure_valid_sqlite_version(); @@ -171,7 +171,8 @@ impl InnerConnection { } pub fn execute_batch(&mut self, sql: &str) -> Result<()> { - let c_sql = str_to_cstring(sql)?; + // use CString instead of SmallCString because it's probably big. + let c_sql = std::ffi::CString::new(sql)?; unsafe { let r = ffi::sqlite3_exec( self.db(), @@ -196,7 +197,7 @@ impl InnerConnection { unsafe { let mut errmsg: *mut c_char = ptr::null_mut(); let r = if let Some(entry_point) = entry_point { - let c_entry = str_to_cstring(entry_point)?; + let c_entry = crate::str_to_cstring(entry_point)?; ffi::sqlite3_load_extension( self.db, dylib_str.as_ptr(), diff --git a/src/lib.rs b/src/lib.rs index 7dc7b03..95da898 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -129,6 +129,7 @@ mod version; pub mod vtab; pub(crate) mod util; +pub(crate) use util::SmallCString; // Number of cached prepared statements we'll hold on to. const STATEMENT_CACHE_DEFAULT_CAPACITY: usize = 16; @@ -233,8 +234,8 @@ unsafe fn errmsg_to_string(errmsg: *const c_char) -> String { String::from_utf8_lossy(c_slice).into_owned() } -fn str_to_cstring(s: &str) -> Result { - Ok(CString::new(s)?) +fn str_to_cstring(s: &str) -> Result { + Ok(SmallCString::new(s)?) } /// Returns `Ok((string ptr, len as c_int, SQLITE_STATIC | SQLITE_TRANSIENT))` @@ -301,7 +302,7 @@ pub enum DatabaseName<'a> { feature = "modern_sqlite" ))] impl DatabaseName<'_> { - fn to_cstring(&self) -> Result { + fn to_cstring(&self) -> Result { use self::DatabaseName::{Attached, Main, Temp}; match *self { Main => str_to_cstring("main"), diff --git a/src/session.rs b/src/session.rs index ada6e69..b4782b2 100644 --- a/src/session.rs +++ b/src/session.rs @@ -102,10 +102,11 @@ impl Session<'_> { /// Attach a table. `None` means all tables. pub fn attach(&mut self, table: Option<&str>) -> Result<()> { let table = if let Some(table) = table { - str_to_cstring(table)?.as_ptr() + Some(str_to_cstring(table)?) } else { - ptr::null() + None }; + let table = table.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null()); unsafe { check!(ffi::sqlite3session_attach(self.s, table)) }; Ok(()) } @@ -156,7 +157,8 @@ impl Session<'_> { /// Load the difference between tables. pub fn diff(&mut self, from: DatabaseName<'_>, table: &str) -> Result<()> { let from = from.to_cstring()?; - let table = str_to_cstring(table)?.as_ptr(); + let table = str_to_cstring(table)?; + let table = table.as_ptr(); unsafe { let mut errmsg = ptr::null_mut(); let r =