Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not panic when PrepareOk fails to decode #2572

Merged
merged 1 commit into from
Jun 30, 2023
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
20 changes: 13 additions & 7 deletions sqlx-mysql/src/protocol/statement/prepare_ok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,28 @@ pub(crate) struct PrepareOk {
}

impl Decode<'_, Capabilities> for PrepareOk {
fn decode_with(mut buf: Bytes, _: Capabilities) -> Result<Self, Error> {
let status = buf.get_u8();
fn decode_with(buf: Bytes, _: Capabilities) -> Result<Self, Error> {
const SIZE: usize = 12;

let mut slice = buf.get(..SIZE).ok_or_else(|| {
err_protocol!("PrepareOk expected 12 bytes but got {} bytes", buf.len())
})?;

let status = slice.get_u8();
if status != 0x00 {
return Err(err_protocol!(
"expected 0x00 (COM_STMT_PREPARE_OK) but found 0x{:02x}",
status
));
}

let statement_id = buf.get_u32_le();
let columns = buf.get_u16_le();
let params = buf.get_u16_le();
let statement_id = slice.get_u32_le();
let columns = slice.get_u16_le();
let params = slice.get_u16_le();

buf.advance(1); // reserved: string<1>
slice.advance(1); // reserved: string<1>

let warnings = buf.get_u16_le();
let warnings = slice.get_u16_le();

Ok(Self {
statement_id,
Expand Down