Make connect/create/declare_vtab safe

This commit is contained in:
gwenn 2018-06-20 20:21:26 +02:00
parent afeb5d4d47
commit eaee342025
4 changed files with 10 additions and 18 deletions

View File

@ -62,11 +62,7 @@ impl Module for ArrayModule {
self.0 self.0
} }
unsafe fn connect( fn connect(db: &mut ffi::sqlite3, _aux: Option<&()>, _args: &[&[u8]]) -> Result<ArrayTab> {
db: *mut ffi::sqlite3,
_aux: Option<&()>,
_args: &[&[u8]],
) -> Result<ArrayTab> {
let vtab = ArrayTab { let vtab = ArrayTab {
base: Default::default(), base: Default::default(),
}; };

View File

@ -84,7 +84,7 @@ impl Module for CSVModule {
self.0 self.0
} }
unsafe fn connect(db: *mut ffi::sqlite3, _aux: Option<&()>, args: &[&[u8]]) -> Result<CSVTab> { fn connect(db: &mut ffi::sqlite3, _aux: Option<&()>, args: &[&[u8]]) -> Result<CSVTab> {
if args.len() < 4 { if args.len() < 4 {
return Err(Error::ModuleError("no CSV file specified".to_owned())); return Err(Error::ModuleError("no CSV file specified".to_owned()));
} }

View File

@ -50,8 +50,8 @@ pub trait Module {
/// Create a new instance of a virtual table in response to a CREATE VIRTUAL TABLE statement. /// Create a new instance of a virtual table in response to a CREATE VIRTUAL TABLE statement.
/// The `db` parameter is a pointer to the SQLite database connection that is executing /// The `db` parameter is a pointer to the SQLite database connection that is executing
/// the CREATE VIRTUAL TABLE statement. /// the CREATE VIRTUAL TABLE statement.
unsafe fn create( fn create(
db: *mut ffi::sqlite3, db: &mut ffi::sqlite3,
aux: Option<&Self::Aux>, aux: Option<&Self::Aux>,
args: &[&[u8]], args: &[&[u8]],
) -> Result<Self::Table> { ) -> Result<Self::Table> {
@ -59,8 +59,8 @@ pub trait Module {
} }
/// Similar to `create`. The difference is that `connect` is called to establish a new connection /// Similar to `create`. The difference is that `connect` is called to establish a new connection
/// to an _existing_ virtual table whereas `create` is called to create a new virtual table from scratch. /// to an _existing_ virtual table whereas `create` is called to create a new virtual table from scratch.
unsafe fn connect( fn connect(
db: *mut ffi::sqlite3, db: &mut ffi::sqlite3,
aux: Option<&Self::Aux>, aux: Option<&Self::Aux>,
args: &[&[u8]], args: &[&[u8]],
) -> Result<Self::Table>; ) -> Result<Self::Table>;
@ -353,9 +353,9 @@ impl InnerConnection {
} }
/// Declare the schema of a virtual table. /// Declare the schema of a virtual table.
pub unsafe fn declare_vtab(db: *mut ffi::sqlite3, sql: &str) -> Result<()> { pub fn declare_vtab(db: &mut ffi::sqlite3, sql: &str) -> Result<()> {
let c_sql = try!(CString::new(sql)); let c_sql = try!(CString::new(sql));
let rc = ffi::sqlite3_declare_vtab(db, c_sql.as_ptr()); let rc = unsafe { ffi::sqlite3_declare_vtab(db, c_sql.as_ptr()) };
if rc == ffi::SQLITE_OK { if rc == ffi::SQLITE_OK {
Ok(()) Ok(())
} else { } else {
@ -572,7 +572,7 @@ macro_rules! create_or_connect {
.iter() .iter()
.map(|&cs| CStr::from_ptr(cs).to_bytes()) // FIXME .to_str() -> Result<&str, Utf8Error> .map(|&cs| CStr::from_ptr(cs).to_bytes()) // FIXME .to_str() -> Result<&str, Utf8Error>
.collect::<Vec<_>>(); .collect::<Vec<_>>();
match $module::$module_func(db, aux.as_ref(), &vec[..]) { match $module::$module_func(db.as_mut().expect("non null db pointer"), aux.as_ref(), &vec[..]) {
Ok(vtab) => { Ok(vtab) => {
let boxed_vtab: *mut $vtab = Box::into_raw(Box::new(vtab)); let boxed_vtab: *mut $vtab = Box::into_raw(Box::new(vtab));
*pp_vtab = boxed_vtab as *mut ffi::sqlite3_vtab; *pp_vtab = boxed_vtab as *mut ffi::sqlite3_vtab;

View File

@ -45,11 +45,7 @@ impl Module for Series {
self.0 self.0
} }
unsafe fn connect( fn connect(db: &mut ffi::sqlite3, _aux: Option<&()>, _args: &[&[u8]]) -> Result<SeriesTab> {
db: *mut ffi::sqlite3,
_aux: Option<&()>,
_args: &[&[u8]],
) -> Result<SeriesTab> {
let vtab = SeriesTab { let vtab = SeriesTab {
base: Default::default(), base: Default::default(),
}; };