validate trailing byte for i64

This commit is contained in:
NikVolf
2018-03-07 13:37:14 +03:00
parent 6dc7f54ab8
commit 4d4dfb621e
2 changed files with 10 additions and 0 deletions

Binary file not shown.

View File

@ -40,6 +40,8 @@ impl Deserialize for VarUint32 {
let mut shift = 0; let mut shift = 0;
let mut u8buf = [0u8; 1]; let mut u8buf = [0u8; 1];
loop { loop {
if shift > 31 { return Err(Error::InvalidVarUint32); }
reader.read_exact(&mut u8buf)?; reader.read_exact(&mut u8buf)?;
let b = u8buf[0] as u32; let b = u8buf[0] as u32;
res |= (b & 0x7f).checked_shl(shift).ok_or(Error::InvalidVarUint32)?; res |= (b & 0x7f).checked_shl(shift).ok_or(Error::InvalidVarUint32)?;
@ -91,6 +93,8 @@ impl Deserialize for VarUint64 {
let mut shift = 0; let mut shift = 0;
let mut u8buf = [0u8; 1]; let mut u8buf = [0u8; 1];
loop { loop {
if shift > 63 { return Err(Error::InvalidVarUint64); }
reader.read_exact(&mut u8buf)?; reader.read_exact(&mut u8buf)?;
let b = u8buf[0] as u64; let b = u8buf[0] as u64;
res |= (b & 0x7f).checked_shl(shift).ok_or(Error::InvalidVarUint64)?; res |= (b & 0x7f).checked_shl(shift).ok_or(Error::InvalidVarUint64)?;
@ -349,6 +353,12 @@ impl Deserialize for VarInt64 {
if (b >> 7) == 0 { if (b >> 7) == 0 {
if shift < 64 && b & 0b0100_0000 == 0b0100_0000 { if shift < 64 && b & 0b0100_0000 == 0b0100_0000 {
res |= (1i64 << shift).wrapping_neg(); res |= (1i64 << shift).wrapping_neg();
} else if shift >= 64 && b & 0b0100_0000 == 0b0100_0000 {
if (b | 0b1000_0000) as i8 != -1 {
return Err(Error::InvalidVarInt64);
}
} else if shift >= 64 && b != 0 {
return Err(Error::InvalidVarInt64);
} }
break; break;
} }