Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions powersync/src/sync/download/sync_iteration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,21 @@ impl DownloadEvent {
let (op, arg) = self.into_powersync_control_argument();

stmt.bind_text(1, op, Destructor::STATIC)?;
arg.bind_to(&stmt, 2)?;
// SAFETY: `arg` remains alive until after `stmt` is explicitly dropped below.
unsafe { arg.bind_to(&stmt, 2)? };

if let ResultCode::ROW = stmt.step()? {
let instructions = if let ResultCode::ROW = stmt.step()? {
let instructions = stmt.column_text(0).map_err(|_| {
PowerSyncError::argument_error("Could not read powersync_control instructions")
})?;

serde_json::from_str(instructions)?
} else {
panic!("Expected a row") // Can't happen, scalar select
}
};

drop(stmt);
instructions
};

tx.commit()?;
Expand All @@ -219,9 +223,10 @@ enum PowerSyncControlArgument {
}

impl PowerSyncControlArgument {
fn bind_to(&self, stmt: &ManagedStmt, index: i32) -> Result<(), ResultCode> {
// We use Destructor::STATIC here which is technically not safe, but fine since we'll always
// drop the statement before the control argument.
/// # Safety
///
/// The argument must outlive `stmt`.
unsafe fn bind_to(&self, stmt: &ManagedStmt, index: i32) -> Result<(), ResultCode> {
match self {
PowerSyncControlArgument::Null => stmt.bind_null(index),
PowerSyncControlArgument::StaticString(str) => {
Expand Down
Loading