Skip to content

Commit

Permalink
always check for allocation failure. switch to panic in case of failu…
Browse files Browse the repository at this point in the history
…re so we can test
  • Loading branch information
droundy committed Sep 15, 2022
1 parent c82bcc0 commit 1baad6a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
26 changes: 17 additions & 9 deletions src/setu32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,8 +952,8 @@ impl Clone for SetU32 {
let c = self.capacity();
unsafe {
let ptr = std::alloc::alloc_zeroed(layout_for_capacity(c)) as *mut S;
if ptr == std::ptr::null_mut() {
std::alloc::handle_alloc_error(layout_for_capacity(c));
if ptr.is_null() {
panic!("memory allocation failed");
}
std::ptr::copy_nonoverlapping(
self.0 as *const u8,
Expand Down Expand Up @@ -990,8 +990,8 @@ impl SetU32 {
let c = other.capacity();
unsafe {
let ptr = std::alloc::alloc_zeroed(layout_for_capacity(c)) as *mut S;
if ptr == std::ptr::null_mut() {
std::alloc::handle_alloc_error(layout_for_capacity(c));
if ptr.is_null() {
panic!("memory allocation failed");
}
(*ptr).b.cap = (*other.0).b.cap;
(*ptr).b.bits = (*other.0).b.bits;
Expand Down Expand Up @@ -1089,8 +1089,8 @@ impl SetU32 {
layout_for_capacity(oldcap as usize),
bytes_for_capacity(cap as usize),
) as *mut S;
if self.0 as usize == 0 {
std::alloc::handle_alloc_error(layout_for_capacity(cap as usize));
if self.0.is_null() {
panic!("memory allocation failed");
}
(*self.0).b.cap = cap;
(*self.0).b.sz += 1;
Expand All @@ -1109,7 +1109,11 @@ impl SetU32 {
let cap = 1 + mx / 32 + mx / 128;
// This should be stored in a dense bitset.
unsafe {
let x = SetU32(std::alloc::alloc_zeroed(layout_for_capacity(cap as usize)) as *mut S);
let ptr = std::alloc::alloc_zeroed(layout_for_capacity(cap as usize)) as *mut S;
if ptr.is_null() {
panic!("memory allocation failed");
}
let x = SetU32(ptr);
(*x.0).b.cap = cap;
(*x.0).b.bits = 32;
x
Expand All @@ -1128,7 +1132,11 @@ impl SetU32 {
pub fn with_capacity_and_bits(cap: usize, bits: u32) -> SetU32 {
if cap > 0 {
unsafe {
let x = SetU32(std::alloc::alloc_zeroed(layout_for_capacity(cap)) as *mut S);
let ptr = std::alloc::alloc_zeroed(layout_for_capacity(cap)) as *mut S;
if ptr.is_null() {
panic!("memory allocation failed");
}
let x = SetU32(ptr);
(*x.0).b.cap = cap as u32;
(*x.0).b.bits = if bits == 0 {
let mut b = 0;
Expand Down Expand Up @@ -2166,4 +2174,4 @@ fn test_remove() {
#[should_panic]
fn test_alloc_failure() {
SetU32::with_capacity_and_bits(usize::MAX / 8 - 2, 0);
}
}
20 changes: 14 additions & 6 deletions src/setu64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,8 @@ impl Clone for SetU64 {
let c = self.capacity();
unsafe {
let ptr = std::alloc::alloc_zeroed(layout_for_capacity(c)) as *mut S;
if ptr == std::ptr::null_mut() {
std::alloc::handle_alloc_error(layout_for_capacity(c));
if ptr.is_null() {
panic!("memory allocation failed");
}
std::ptr::copy_nonoverlapping(
self.0 as *const u8,
Expand Down Expand Up @@ -743,8 +743,8 @@ impl SetU64 {
let c = other.capacity();
unsafe {
let ptr = std::alloc::alloc_zeroed(layout_for_capacity(c)) as *mut S;
if ptr == std::ptr::null_mut() {
std::alloc::handle_alloc_error(layout_for_capacity(c));
if ptr.is_null() {
panic!("memory allocation failed");
}
(*ptr).b.cap = (*other.0).b.cap;
(*ptr).b.bits = (*other.0).b.bits;
Expand Down Expand Up @@ -841,7 +841,11 @@ impl SetU64 {
let cap = 1 + mx / 64 + mx / 256;
// This should be stored in a dense bitset.
unsafe {
let x = SetU64(std::alloc::alloc_zeroed(layout_for_capacity(cap as usize)) as *mut S);
let ptr = std::alloc::alloc_zeroed(layout_for_capacity(cap as usize)) as *mut S;
if ptr.is_null() {
panic!("memory allocation failed");
}
let x = SetU64(ptr);
(*x.0).b.cap = cap as usize;
(*x.0).b.bits = 64;
x
Expand All @@ -860,7 +864,11 @@ impl SetU64 {
pub fn with_capacity_and_bits(cap: usize, bits: u64) -> SetU64 {
if cap > 0 {
unsafe {
let x = SetU64(std::alloc::alloc_zeroed(layout_for_capacity(cap)) as *mut S);
let ptr = std::alloc::alloc_zeroed(layout_for_capacity(cap)) as *mut S;
if ptr.is_null() {
panic!("memory allocation failed");
}
let x = SetU64(ptr);
(*x.0).b.cap = cap;
(*x.0).b.bits = if bits == 0 {
let mut b = 0;
Expand Down

0 comments on commit 1baad6a

Please sign in to comment.