From ca25fd4accd83736a86f32e60a16ba13b6c323e7 Mon Sep 17 00:00:00 2001 From: Sieger9303 <1517158051@qq.com> Date: Fri, 22 May 2026 14:27:10 +0800 Subject: [PATCH 1/4] fix: reject excessive AllocRingBuffer allocations --- src/with_alloc/alloc_ringbuffer.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/with_alloc/alloc_ringbuffer.rs b/src/with_alloc/alloc_ringbuffer.rs index c64ecbd..b3bb514 100644 --- a/src/with_alloc/alloc_ringbuffer.rs +++ b/src/with_alloc/alloc_ringbuffer.rs @@ -321,9 +321,19 @@ impl AllocRingBuffer { #[must_use] pub fn new(capacity: usize) -> Self { assert_ne!(capacity, 0, "Capacity must be greater than 0"); - let size = capacity.next_power_of_two(); - let layout = alloc::alloc::Layout::array::(size).unwrap(); - let buf = unsafe { alloc::alloc::alloc(layout).cast() }; + let size = capacity + .checked_next_power_of_two() + .expect("Capacity is too large"); + let layout = alloc::alloc::Layout::array::(size) + .expect("Capacity is too large"); + assert!( + layout.size() <= (1usize << 40), + "Capacity is too large" + ); + let buf: *mut T = unsafe { alloc::alloc::alloc(layout).cast() }; + if buf.is_null() { + alloc::alloc::handle_alloc_error(layout); + } Self { buf, size, From f0be7a39f68275a716cbc44bed7eb4df125afe33 Mon Sep 17 00:00:00 2001 From: Vivian Date: Sat, 23 May 2026 15:11:49 +0200 Subject: [PATCH 2/4] fmt --- src/with_alloc/alloc_ringbuffer.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/with_alloc/alloc_ringbuffer.rs b/src/with_alloc/alloc_ringbuffer.rs index b3bb514..19c1100 100644 --- a/src/with_alloc/alloc_ringbuffer.rs +++ b/src/with_alloc/alloc_ringbuffer.rs @@ -322,14 +322,10 @@ impl AllocRingBuffer { pub fn new(capacity: usize) -> Self { assert_ne!(capacity, 0, "Capacity must be greater than 0"); let size = capacity - .checked_next_power_of_two() - .expect("Capacity is too large"); - let layout = alloc::alloc::Layout::array::(size) + .checked_next_power_of_two() .expect("Capacity is too large"); - assert!( - layout.size() <= (1usize << 40), - "Capacity is too large" - ); + let layout = alloc::alloc::Layout::array::(size).expect("Capacity is too large"); + assert!(layout.size() <= (1usize << 40), "Capacity is too large"); let buf: *mut T = unsafe { alloc::alloc::alloc(layout).cast() }; if buf.is_null() { alloc::alloc::handle_alloc_error(layout); From 52360385a8d594e0ad8a8421ee3719c6c20b6590 Mon Sep 17 00:00:00 2001 From: Vivian Date: Sat, 23 May 2026 16:45:46 +0200 Subject: [PATCH 3/4] chore: remove compiletests --- Cargo.toml | 1 - .../test_const_generic_array_zero_length.rs | 9 -------- ...est_const_generic_array_zero_length_new.rs | 10 -------- tests/compiletests.rs | 23 ------------------- 4 files changed, 43 deletions(-) delete mode 100644 tests/compile-fail/test_const_generic_array_zero_length.rs delete mode 100644 tests/compile-fail/test_const_generic_array_zero_length_new.rs delete mode 100644 tests/compiletests.rs diff --git a/Cargo.toml b/Cargo.toml index a705078..434eebe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ license = "MIT" [dev-dependencies] criterion = { version = "0.4.0", features = ["html_reports"] } -compiletest_rs = "0.10.0" [features] default = ["alloc"] diff --git a/tests/compile-fail/test_const_generic_array_zero_length.rs b/tests/compile-fail/test_const_generic_array_zero_length.rs deleted file mode 100644 index 3b69f1a..0000000 --- a/tests/compile-fail/test_const_generic_array_zero_length.rs +++ /dev/null @@ -1,9 +0,0 @@ -extern crate ringbuffer; - -use ringbuffer::ConstGenericRingBuffer; - -fn main() { - let _ = ConstGenericRingBuffer::::new(); - //~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::::new::<0>` - // ringbuffer can't be zero length -} diff --git a/tests/compile-fail/test_const_generic_array_zero_length_new.rs b/tests/compile-fail/test_const_generic_array_zero_length_new.rs deleted file mode 100644 index bdaddbf..0000000 --- a/tests/compile-fail/test_const_generic_array_zero_length_new.rs +++ /dev/null @@ -1,10 +0,0 @@ -extern crate ringbuffer; - -use ringbuffer::{ConstGenericRingBuffer, RingBuffer}; - -fn main() { - let mut buf = ConstGenericRingBuffer::new::<0>(); - //~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::::new::<0>` - // ringbuffer can't be zero length -let _ = buf.enqueue(5); -} diff --git a/tests/compiletests.rs b/tests/compiletests.rs deleted file mode 100644 index f48163e..0000000 --- a/tests/compiletests.rs +++ /dev/null @@ -1,23 +0,0 @@ -extern crate compiletest_rs as compiletest; - -use std::path::PathBuf; - -#[cfg(test)] -mod conversions; - -fn run_mode(mode: &'static str) { - let mut config = compiletest::Config::default(); - - config.mode = mode.parse().expect("Invalid mode"); - config.src_base = PathBuf::from(format!("tests/{}", mode)); - config.link_deps(); // Populate config.target_rustcflags with dependencies on the path - config.clean_rmeta(); // If your tests import the parent crate, this helps with E0464 - - compiletest::run_tests(&config); -} - -#[test] -#[cfg_attr(miri, ignore)] -fn compile_test() { - run_mode("compile-fail"); -} From 4e6e871ea2857255e2e549729d971d86bd646f5d Mon Sep 17 00:00:00 2001 From: Vivian Date: Sat, 23 May 2026 17:02:32 +0200 Subject: [PATCH 4/4] check only on 64 bit --- src/with_alloc/alloc_ringbuffer.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/with_alloc/alloc_ringbuffer.rs b/src/with_alloc/alloc_ringbuffer.rs index 19c1100..3b73270 100644 --- a/src/with_alloc/alloc_ringbuffer.rs +++ b/src/with_alloc/alloc_ringbuffer.rs @@ -325,6 +325,7 @@ impl AllocRingBuffer { .checked_next_power_of_two() .expect("Capacity is too large"); let layout = alloc::alloc::Layout::array::(size).expect("Capacity is too large"); + #[cfg(target_pointer_width = "64")] assert!(layout.size() <= (1usize << 40), "Capacity is too large"); let buf: *mut T = unsafe { alloc::alloc::alloc(layout).cast() }; if buf.is_null() {