Skip to content

Commit

Permalink
drop vtab structure on error creating the vtab
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Aug 15, 2023
1 parent b4cb135 commit 2ff116a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
27 changes: 24 additions & 3 deletions core/rs/core/src/create_cl_set_vtab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,18 @@ extern "C" fn create(
err: *mut *mut c_char,
) -> c_int {
match create_impl(db, argc, argv, vtab, err) {
Ok(rc) | Err(rc) => rc as c_int,
Ok(rc) => rc as c_int,
Err(rc) => {
// deallocate the vtab on error.
unsafe {
if *vtab != core::ptr::null_mut() {
let tab = Box::from_raw((*vtab).cast::<CLSetTab>());
drop(tab);
*vtab = core::ptr::null_mut();
}
}
rc as c_int
}
}
}

Expand Down Expand Up @@ -98,7 +109,17 @@ extern "C" fn connect(
Ok(vtab_args) => match connect_create_shared(db, vtab, &vtab_args) {
Ok(rc) | Err(rc) => rc as c_int,
},
Err(_e) => ResultCode::FORMAT as c_int,
Err(_e) => {
// free the tab if it was allocated
unsafe {
if *vtab != core::ptr::null_mut() {
let tab = Box::from_raw((*vtab).cast::<CLSetTab>());
drop(tab);
*vtab = core::ptr::null_mut();
}
};
ResultCode::FORMAT as c_int
}
}
}

Expand Down Expand Up @@ -130,7 +151,7 @@ extern "C" fn best_index(_vtab: *mut sqlite::vtab, _index_info: *mut sqlite::ind
}

extern "C" fn disconnect(vtab: *mut sqlite::vtab) -> c_int {
unsafe {
if vtab != core::ptr::null_mut() {
let tab = unsafe { Box::from_raw(vtab.cast::<CLSetTab>()) };
drop(tab);
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/rust.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// structures to the old C-code that hasn't been converted yet.
// These are those definitions.

int crsql_backfill_table(sqlite3_context *context, const char *tblName,
int crsql_backfill_table(sqlite3 *db, const char *tblName,
const char **zpkNames, int pkCount,
const char **zNonPkNames, int nonPkCount,
int isCommitAlter, int noTx);
Expand Down

0 comments on commit 2ff116a

Please sign in to comment.