Skip to content

Add assert in ring_buffer.rs#1537

Closed
yilin0518 wants to merge 1 commit into
DragonOS-Community:masterfrom
yilin0518:master
Closed

Add assert in ring_buffer.rs#1537
yilin0518 wants to merge 1 commit into
DragonOS-Community:masterfrom
yilin0518:master

Conversation

@yilin0518

Copy link
Copy Markdown

Add assert to prevent panic in Vec::with_capacity

Copilot AI review requested due to automatic review settings December 22, 2025 15:47
@github-actions github-actions Bot added the ambiguous The title of PR/issue doesn't match the format label Dec 22, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an assertion to the RingBuffer::new constructor to prevent panics in Vec::with_capacity by checking that the capacity doesn't exceed isize::MAX. This is a defensive programming measure to catch potential allocation failures early.

Key Changes:

  • Added validation to prevent capacity-related panics in Vec::with_capacity

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +42 to +43
assert!(capacity < isize::MAX as usize, "capacity must be less than isize::MAX");

Copilot AI Dec 22, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion condition is incorrect. According to Rust's documentation, Vec::with_capacity panics if the capacity in bytes (not the number of elements) exceeds isize::MAX. The correct check should account for the size of type T: capacity * core::mem::size_of::<T>() <= isize::MAX as usize. The current assertion checks only the element count, which may be too restrictive for small types or insufficient for large types.

Suggested change
assert!(capacity < isize::MAX as usize, "capacity must be less than isize::MAX");
let elem_size = core::mem::size_of::<T>();
assert!(
capacity
.checked_mul(elem_size)
.map_or(false, |bytes| bytes <= isize::MAX as usize),
"capacity * size_of::<T>() must be <= isize::MAX"
);

Copilot uses AI. Check for mistakes.
"capacity must be a power of two"
);

assert!(capacity < isize::MAX as usize, "capacity must be less than isize::MAX");

Copilot AI Dec 22, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message should be more specific about why this constraint exists and reference the underlying Vec::with_capacity limitation. Consider changing it to something like "capacity in bytes would exceed isize::MAX" to make the requirement clearer to developers.

Suggested change
assert!(capacity < isize::MAX as usize, "capacity must be less than isize::MAX");
assert!(
capacity < isize::MAX as usize,
"capacity in bytes would exceed isize::MAX (Vec::with_capacity limitation)"
);

Copilot uses AI. Check for mistakes.
@fslongjin

Copy link
Copy Markdown
Member

这个assert是不必要的,因为内存管理会报错。
而且,assert会带来内核panic,我们鼓励使用正确的错误处理而不是panic

@yilin0518 yilin0518 closed this Dec 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ambiguous The title of PR/issue doesn't match the format

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants