Skip to content

Commit

Permalink
add test before using Layout::from_size_align_unchecked to avoid UB (f…
Browse files Browse the repository at this point in the history
…ixes #22)
  • Loading branch information
droundy committed Oct 12, 2024
1 parent a322e0e commit 75a0519
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/copyset.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(test)]
pub trait CopySet: Default + Clone {
type Item: Copy + Eq + Ord + std::fmt::Display + std::fmt::Debug;
type Iter: Iterator<Item = Self::Item>;
Expand Down Expand Up @@ -141,6 +142,7 @@ assert_eq!(a | &b, (1..6).collect());

pub(crate) use impl_set_methods;

#[cfg(test)]
impl CopySet for std::collections::HashSet<u64> {
type Item = u64;
type Iter = std::collections::hash_set::IntoIter<u64>;
Expand All @@ -164,6 +166,7 @@ impl CopySet for std::collections::HashSet<u64> {
}
}

#[cfg(test)]
impl CopySet for std::collections::HashSet<u32> {
type Item = u32;
type Iter = std::collections::hash_set::IntoIter<u32>;
Expand Down
1 change: 1 addition & 0 deletions src/set64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ impl<'a, 'b, T: Fits64> std::ops::BitOr<&'b Set64<T>> for &'a Set64<T> {
}
}

#[cfg(test)]
impl<T: Fits64 + Eq + Ord + std::fmt::Debug + std::fmt::Display> crate::copyset::CopySet
for Set64<T>
{
Expand Down
5 changes: 5 additions & 0 deletions src/setu32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ enum InternalMut<'a> {
},
}

#[cfg(test)]
impl crate::copyset::CopySet for SetU32 {
type Item = u32;
type Iter = iter::IntoIter;
Expand Down Expand Up @@ -1302,6 +1303,10 @@ fn bytes_for_capacity(sz: usize) -> usize {
sz * 4 + std::mem::size_of::<S>() - 4
}
fn layout_for_capacity(sz: usize) -> std::alloc::Layout {
let size = bytes_for_capacity(sz);
if size >= usize::MAX / 2 {
panic!("tinyset size is too large: {}", sz);
}
unsafe { std::alloc::Layout::from_size_align_unchecked(bytes_for_capacity(sz), 4) }
}

Expand Down
7 changes: 6 additions & 1 deletion src/setu64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,7 @@ impl SetU64 {
}
}

#[cfg(test)]
impl crate::copyset::CopySet for SetU64 {
type Item = u64;
type Iter = IntoIter;
Expand Down Expand Up @@ -1383,7 +1384,11 @@ fn bytes_for_capacity(sz: usize) -> usize {
sz * 8 + std::mem::size_of::<S>() - 8
}
fn layout_for_capacity(sz: usize) -> std::alloc::Layout {
unsafe { std::alloc::Layout::from_size_align_unchecked(bytes_for_capacity(sz), 8) }
let size = bytes_for_capacity(sz);
if size >= usize::MAX / 2 {
panic!("tinyset size is too large: {}", sz);
}
unsafe { std::alloc::Layout::from_size_align_unchecked(size, 8) }
}

impl Drop for SetU64 {
Expand Down
1 change: 1 addition & 0 deletions src/setusize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ fn serialize_deserialize() {
assert_eq!(set, serde_json::from_str(&s).unwrap());
}

#[cfg(test)]
impl crate::copyset::CopySet for SetUsize {
type Item = usize;
type Iter = IntoIter;
Expand Down

0 comments on commit 75a0519

Please sign in to comment.