Skip to content

Commit 4e2fbe5

Browse files
committed
Better errors on bad handshakes
1 parent 9daaa16 commit 4e2fbe5

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/lib.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,34 @@ impl<B: MysqlShim<W>, R: Read, W: Write> MysqlIntermediary<B, R, W> {
253253
self.writer.flush()?;
254254

255255
{
256-
let (seq, handshake) = self.reader.next()?.unwrap();
257-
let _handshake = commands::client_handshake(&handshake).unwrap().1;
256+
let (seq, handshake) = self.reader.next()?.ok_or_else(|| {
257+
io::Error::new(
258+
io::ErrorKind::ConnectionAborted,
259+
"peer terminated connection",
260+
)
261+
})?;
262+
let _handshake = commands::client_handshake(&handshake)
263+
.map_err(|e| match e {
264+
nom::Err::Incomplete(_) => io::Error::new(
265+
io::ErrorKind::UnexpectedEof,
266+
"client sent incomplete handshake",
267+
),
268+
nom::Err::Failure((input, nom_e_kind))
269+
| nom::Err::Error((input, nom_e_kind)) => {
270+
if let nom::error::ErrorKind::Eof = nom_e_kind {
271+
io::Error::new(
272+
io::ErrorKind::UnexpectedEof,
273+
format!("client did not complete handshake; got {:?}", input),
274+
)
275+
} else {
276+
io::Error::new(
277+
io::ErrorKind::InvalidData,
278+
format!("bad client handshake; got {:?} ({:?})", input, nom_e_kind),
279+
)
280+
}
281+
}
282+
})?
283+
.1;
258284
self.writer.set_seq(seq + 1);
259285
}
260286

0 commit comments

Comments
 (0)