Skip to content

Commit

Permalink
Merge pull request #271 from kirk-baird/bitmap-deser-overflow
Browse files Browse the repository at this point in the history
Fix potential overflow in deserialization of Bitmap
  • Loading branch information
Kerollmops authored Apr 30, 2024
2 parents 946bbfd + bf0cb5a commit 79c031d
Show file tree
Hide file tree
Showing 23 changed files with 26 additions and 49 deletions.
2 changes: 0 additions & 2 deletions src/bitmap/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ mod test {
use crate::bitmap::container::Container;
use crate::bitmap::store::{ArrayStore, BitmapStore, Store};
use crate::RoaringBitmap;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::fmt::{Debug, Formatter};
use proptest::bits::{BitSetLike, BitSetStrategy, SampledBitSetStrategy};
use proptest::collection::{vec, SizeRange};
Expand Down
2 changes: 0 additions & 2 deletions src/bitmap/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use core::ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, RangeInclusive, Sub, SubAssign,
};

use alloc::vec::Vec;

use super::store::{self, Store};
use super::util;

Expand Down
2 changes: 0 additions & 2 deletions src/bitmap/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use core::fmt;

use alloc::vec::Vec;

use crate::RoaringBitmap;

impl fmt::Debug for RoaringBitmap {
Expand Down
2 changes: 0 additions & 2 deletions src/bitmap/inherent.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use core::cmp::Ordering;
use core::ops::RangeBounds;

use alloc::vec::Vec;

use crate::RoaringBitmap;

use super::container::Container;
Expand Down
4 changes: 2 additions & 2 deletions src/bitmap/iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloc::vec::{self, Vec};
use core::iter::{self, FromIterator};
use alloc::vec;
use core::iter;
use core::slice;

use super::container::Container;
Expand Down
2 changes: 0 additions & 2 deletions src/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ mod serde;
#[cfg(feature = "std")]
mod serialization;

use alloc::vec::Vec;

use self::cmp::Pairs;
pub use self::iter::IntoIter;
pub use self::iter::Iter;
Expand Down
2 changes: 1 addition & 1 deletion src/bitmap/multiops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::{
ops::{BitOrAssign, BitXorAssign},
};

use alloc::{borrow::Cow, vec::Vec};
use alloc::borrow::Cow;

use crate::{MultiOps, RoaringBitmap};

Expand Down
2 changes: 0 additions & 2 deletions src/bitmap/ops.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use core::mem;
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};

use alloc::vec::Vec;

use crate::bitmap::container::Container;
use crate::bitmap::Pairs;
use crate::RoaringBitmap;
Expand Down
17 changes: 13 additions & 4 deletions src/bitmap/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bytemuck::cast_slice_mut;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use core::convert::{Infallible, TryFrom};
use core::convert::Infallible;
use core::ops::RangeInclusive;
use std::error::Error;
use std::io;
Expand Down Expand Up @@ -226,9 +226,11 @@ impl RoaringBitmap {

let cardinality = intervals.iter().map(|[_, len]| *len as usize).sum();
let mut store = Store::with_capacity(cardinality);
intervals.into_iter().for_each(|[s, len]| {
store.insert_range(RangeInclusive::new(s, s + len));
});
intervals.into_iter().try_for_each(|[s, len]| -> Result<(), io::ErrorKind> {
let end = s.checked_add(len).ok_or(io::ErrorKind::InvalidData)?;
store.insert_range(RangeInclusive::new(s, end));
Ok(())
})?;
store
} else if cardinality <= ARRAY_LIMIT {
let mut values = vec![0; cardinality as usize];
Expand Down Expand Up @@ -267,4 +269,11 @@ mod test {
prop_assert_eq!(bitmap, RoaringBitmap::deserialize_from(buffer.as_slice()).unwrap());
}
}

#[test]
fn test_deserialize_overflow_s_plus_len() {
let data = vec![59, 48, 0, 0, 255, 130, 254, 59, 48, 2, 0, 41, 255, 255, 166, 197, 4, 0, 2];
let res = RoaringBitmap::deserialize_from(data.as_slice());
assert!(res.is_err());
}
}
4 changes: 0 additions & 4 deletions src/bitmap/store/array_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ mod scalar;
mod vector;
mod visitor;

use alloc::boxed::Box;
use alloc::vec::Vec;

use crate::bitmap::store::array_store::visitor::{CardinalityCounter, VecWriter};
use core::cmp::Ordering;
use core::cmp::Ordering::*;
use core::convert::TryFrom;
use core::fmt::{Display, Formatter};
use core::ops::{BitAnd, BitAndAssign, BitOr, BitXor, RangeInclusive, Sub, SubAssign};

Expand Down
2 changes: 0 additions & 2 deletions src/bitmap/store/array_store/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use alloc::vec::Vec;

#[cfg(feature = "simd")]
use crate::bitmap::store::array_store::vector::swizzle_to_front;

Expand Down
3 changes: 0 additions & 3 deletions src/bitmap/store/bitmap_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use core::cmp::Ordering;
use core::fmt::{Display, Formatter};
use core::ops::{BitAndAssign, BitOrAssign, BitXorAssign, RangeInclusive, SubAssign};

use alloc::boxed::Box;
use alloc::vec::Vec;

use super::ArrayStore;

pub const BITMAP_LENGTH: usize = 1024;
Expand Down
2 changes: 1 addition & 1 deletion src/bitmap/store/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod array_store;
mod bitmap_store;

use alloc::{boxed::Box, vec};
use alloc::vec;
use core::mem;
use core::ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, RangeInclusive, Sub, SubAssign,
Expand Down
2 changes: 0 additions & 2 deletions src/treemap/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use core::fmt;

use alloc::vec::Vec;

use crate::RoaringTreemap;

impl fmt::Debug for RoaringTreemap {
Expand Down
1 change: 0 additions & 1 deletion src/treemap/inherent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use alloc::collections::btree_map::{BTreeMap, Entry};
use alloc::vec::Vec;
use core::iter;
use core::ops::RangeBounds;

Expand Down
2 changes: 1 addition & 1 deletion src/treemap/iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloc::collections::{btree_map, BTreeMap};
use core::iter::{self, FromIterator};
use core::iter;

use super::util;
use crate::bitmap::IntoIter as IntoIter32;
Expand Down
5 changes: 1 addition & 4 deletions src/treemap/multiops.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use alloc::{
collections::{binary_heap::PeekMut, BTreeMap, BinaryHeap},
vec::Vec,
};
use alloc::collections::{binary_heap::PeekMut, BTreeMap, BinaryHeap};
use core::{borrow::Borrow, cmp::Ordering, mem};

use crate::{MultiOps, RoaringBitmap, RoaringTreemap};
Expand Down
1 change: 0 additions & 1 deletion src/treemap/ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use alloc::collections::btree_map::Entry;
use alloc::vec::Vec;
use core::mem;
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};

Expand Down
1 change: 0 additions & 1 deletion tests/iter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::iter::FromIterator;
use proptest::arbitrary::any;
use proptest::collection::btree_set;
use proptest::proptest;
Expand Down
14 changes: 7 additions & 7 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ fn smoke() {
assert!(bitmap.contains(1));
assert_eq!(bitmap.len(), 1);
assert!(!bitmap.is_empty());
bitmap.insert(u32::max_value() - 2);
assert!(bitmap.contains(u32::max_value() - 2));
bitmap.insert(u32::MAX - 2);
assert!(bitmap.contains(u32::MAX - 2));
assert_eq!(bitmap.len(), 2);
bitmap.insert(u32::max_value());
assert!(bitmap.contains(u32::max_value()));
bitmap.insert(u32::MAX);
assert!(bitmap.contains(u32::MAX));
assert_eq!(bitmap.len(), 3);
bitmap.insert(2);
assert!(bitmap.contains(2));
Expand All @@ -28,9 +28,9 @@ fn smoke() {
assert!(!bitmap.contains(0));
assert!(bitmap.contains(1));
assert!(!bitmap.contains(100));
assert!(bitmap.contains(u32::max_value() - 2));
assert!(!bitmap.contains(u32::max_value() - 1));
assert!(bitmap.contains(u32::max_value()));
assert!(bitmap.contains(u32::MAX - 2));
assert!(!bitmap.contains(u32::MAX - 1));
assert!(bitmap.contains(u32::MAX));
}

#[test]
Expand Down
1 change: 0 additions & 1 deletion tests/push.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
extern crate roaring;
use core::iter::FromIterator;
use roaring::{RoaringBitmap, RoaringTreemap};

/// macro created to reduce code duplication
Expand Down
1 change: 0 additions & 1 deletion tests/treemap_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ extern crate roaring;
mod iter;
use roaring::RoaringTreemap;

use core::iter::FromIterator;
use iter::outside_in;
use proptest::arbitrary::any;
use proptest::collection::btree_set;
Expand Down
1 change: 0 additions & 1 deletion tests/treemap_serialization.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![cfg(feature = "std")]

use core::iter::FromIterator;
use roaring::RoaringTreemap;

fn serialize_deserialize<Dataset, I>(dataset: Dataset)
Expand Down

0 comments on commit 79c031d

Please sign in to comment.