Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better size limit for #21 #22

Merged
merged 3 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io;
use std::io::{Read, Result};

use crate::fixed::FixedInt;
use crate::varint::{VarInt, MSB};
use crate::varint::{VarInt, VarIntMaxSize, MSB};

#[cfg(feature = "tokio_async")]
use tokio::io::{AsyncReadExt, AsyncRead};
Expand Down Expand Up @@ -34,12 +34,16 @@ pub trait VarIntAsyncReader {
#[derive(Default)]
pub struct VarIntProcessor {
buf: [u8; 10],
maxsize: usize,
i: usize,
}

impl VarIntProcessor {
fn new<VI: VarIntMaxSize>() -> VarIntProcessor {
VarIntProcessor { maxsize: VI::varint_max_size(), ..VarIntProcessor::default() }
}
fn push(&mut self, b: u8) -> Result<()> {
if self.i >= 10 {
if self.i >= self.maxsize {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Unterminated varint",
Expand All @@ -62,7 +66,7 @@ impl VarIntProcessor {
impl<AR: AsyncRead + Unpin + Send> VarIntAsyncReader for AR {
async fn read_varint_async<VI: VarInt>(&mut self) -> Result<VI> {
let mut buf = [0 as u8; 1];
let mut p = VarIntProcessor::default();
let mut p = VarIntProcessor::new::<VI>();

while !p.finished() {
let read = self.read(&mut buf).await?;
Expand All @@ -86,7 +90,7 @@ impl<AR: AsyncRead + Unpin + Send> VarIntAsyncReader for AR {
impl<R: Read> VarIntReader for R {
fn read_varint<VI: VarInt>(&mut self) -> Result<VI> {
let mut buf = [0 as u8; 1];
let mut p = VarIntProcessor::default();
let mut p = VarIntProcessor::new::<VI>();

while !p.finished() {
let read = self.read(&mut buf)?;
Expand Down
12 changes: 12 additions & 0 deletions src/varint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::mem::size_of;

/// Most-significant byte, == 0x80
pub const MSB: u8 = 0b1000_0000;
/// All bits except for the most significant. Can be used as bitmask to drop the most-signficant
Expand Down Expand Up @@ -64,6 +66,16 @@ fn zigzag_decode(from: u64) -> i64 {
((from >> 1) ^ (-((from & 1) as i64)) as u64) as i64
}

pub(crate) trait VarIntMaxSize {
fn varint_max_size() -> usize;
}

impl<VI: VarInt> VarIntMaxSize for VI {
fn varint_max_size() -> usize {
(size_of::<VI>()*8+7)/7
}
}

macro_rules! impl_varint {
($t:ty, unsigned) => {
impl VarInt for $t {
Expand Down
6 changes: 6 additions & 0 deletions src/varint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,10 @@ mod tests {
encoded.push(0x00);
assert_eq!(i64::decode_var(&encoded[..]), None);
}

#[test]
fn test_regression_22() {
let mut encoded: Vec<u8> = (0x112233 as u64).encode_var_vec();
assert_eq!(encoded.as_slice().read_varint::<i8>().unwrap_err().kind(), std::io::ErrorKind::InvalidData);
}
}