Skip to content

Commit

Permalink
Roll back buffer in MySqlArguments if encoding fails
Browse files Browse the repository at this point in the history
  • Loading branch information
FSMaxB committed Apr 26, 2024
1 parent e53367a commit 1229ee6
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions sqlx-mysql/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ impl MySqlArguments {
{
let ty = value.produces().unwrap_or_else(T::type_info);

self.types.push(ty);
let value_length_before_encoding = self.values.len();
let is_null = match value.encode(&mut self.values) {
Ok(is_null) => is_null,
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);
}
};

let is_null = value.encode(&mut self.values)?;
self.types.push(ty);
self.null_bitmap.push(is_null);

Ok(())
Expand Down

0 comments on commit 1229ee6

Please sign in to comment.