Merge pull request #35 from VersBinarii/fix_bool

Decode boolean: check if enough bytes was received
This commit is contained in:
Austin Bonander 2020-01-07 13:58:11 -08:00 committed by GitHub
commit 0a5aacabd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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),
}
}
}