Check if enough bytes was received

This commit is contained in:
Kris 2020-01-07 11:44:15 +01:00
parent bad21b1567
commit 7464d35803
2 changed files with 12 additions and 4 deletions

View file

@ -19,7 +19,11 @@ impl Encode<MySql> for bool {
impl Decode<MySql> for bool {
fn decode(buf: &[u8]) -> Result<Self, DecodeError> {
// FIXME: Return an error if the buffer size is not (at least) 1
Ok(buf[0] != 0)
match buf.len() {
0 => Err(DecodeError::Message(Box::new(
"Expected minimum 1 byte but received none.",
))),
_ => Ok(buf[0] != 0),
}
}
}

View file

@ -18,7 +18,11 @@ impl Encode<Postgres> for bool {
impl Decode<Postgres> for bool {
fn decode(buf: &[u8]) -> Result<Self, DecodeError> {
// FIXME: Return an error if the buffer size is not (at least) 1
Ok(buf[0] != 0)
match buf.len() {
0 => Err(DecodeError::Message(Box::new(
"Expected minimum 1 byte but received none.",
))),
_ => Ok(buf[0] != 0),
}
}
}