Skip to content

Commit

Permalink
Roll back buffer in SqliteArguments if encoding fails
Browse files Browse the repository at this point in the history
  • Loading branch information
FSMaxB committed Apr 26, 2024
1 parent 1229ee6 commit d73561f
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions sqlx-sqlite/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ impl<'q> SqliteArguments<'q> {
where
T: Encode<'q, Sqlite>,
{
if let IsNull::Yes = value.encode(&mut self.values)? {
self.values.push(SqliteArgumentValue::Null);
}
let value_length_before_encoding = self.values.len();

match value.encode(&mut self.values) {
Ok(IsNull::Yes) => self.values.push(SqliteArgumentValue::Null),
Ok(IsNull::No) => {}
Err(error) => {
// reset the value buffer to its previous value if encoding failed so we don't leave a half-encoded value behind
self.values.truncate(value_length_before_encoding);
return Err(error);
}
};

Ok(())
}
Expand Down

0 comments on commit d73561f

Please sign in to comment.