Skip to content

Commit d0ca316

Browse files
committed
fix(mysql): return error instead of panic on truncated OK packet
1 parent f5cdf33 commit d0ca316

File tree

1 file changed

+16
-0
lines changed
  • sqlx-mysql/src/protocol/response

1 file changed

+16
-0
lines changed

sqlx-mysql/src/protocol/response/ok.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ impl ProtocolDecode<'_> for OkPacket {
2626

2727
let affected_rows = buf.get_uint_lenenc();
2828
let last_insert_id = buf.get_uint_lenenc();
29+
30+
if buf.remaining() < 4 {
31+
return Err(err_protocol!(
32+
"OK_Packet too short: expected at least 4 more bytes for status+warnings, got {}",
33+
buf.remaining()
34+
));
35+
}
36+
2937
let status = Status::from_bits_truncate(buf.get_u16_le());
3038
let warnings = buf.get_u16_le();
3139

@@ -76,3 +84,11 @@ fn test_decode_ok_packet_with_extended_info() {
7684
assert_eq!(p.warnings, 1);
7785
assert!(p.status.contains(Status::SERVER_STATUS_AUTOCOMMIT));
7886
}
87+
88+
#[test]
89+
fn test_decode_ok_packet_truncated() {
90+
const DATA: &[u8] = b"\x00\x00\x00\x01";
91+
92+
let err = OkPacket::decode(DATA.into()).unwrap_err();
93+
assert!(matches!(err, Error::Protocol(_)), "{err}");
94+
}

0 commit comments

Comments
 (0)