Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This reverts commit 6ded34c6186032b7583c26578ccdc9d87aed81c4.
bfdf611 to
0626ac6
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
src/batteries/geometric_stack.cxx (1)
331-337:⚠️ Potential issue | 🟠 MajorCast before adding the header node.
Line 335 adds
1 + ptr->sizeindiff_typebeforesafe_cast, creating a signed overflow risk when a stacklet's stored size approachesstd::numeric_limits<diff_type>::max(). The allocation-side code at line 313 correctly orders the operation as1 + safe_cast<size_type>(num_nodes)(unsigned arithmetic). Mirror this by castingptr->sizetosize_typefirst, then adding in the unsigned domain.Proposed fix
- size_type allocated_nodes = safe_cast<size_type>(1 + ptr->size); + size_type allocated_nodes = size_type{1} + safe_cast<size_type>(ptr->size);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/batteries/geometric_stack.cxx` around lines 331 - 337, The delete_node function computes allocated_nodes using signed arithmetic then casts, which can overflow; change the order so you first cast ptr->size to size_type using safe_cast and then add 1 in the unsigned domain (i.e., allocated_nodes = safe_cast<size_type>(ptr->size) + 1) before calling node_traits::destroy and node_traits::deallocate to mirror the allocation-side ordering and avoid signed overflow.src/batteries/adaptor_stack.cxx (1)
82-94:⚠️ Potential issue | 🟠 MajorAvoid overflow when rounding allocation sizes.
Lines 84 and 93 can overflow
size + (k_new_align - 1)in std::size_t arithmetic beforesafe_castreceives the value, producing incorrect (too-small) allocation/deallocation counts for very large inputs. Thesafe_castbounds check cannot prevent this prior overflow. Use the equivalent non-overflowing form((size - 1) / k_new_align) + 1where the preconditionsize > 0ensuressize - 1stays within range.Proposed fix
[[nodiscard]] constexpr auto push(std::size_t size) -> void_ptr { LF_ASSUME(size > 0); - size_type num_aligned = safe_cast<size_type>((size + (k_new_align - 1)) / k_new_align); + size_type num_aligned = safe_cast<size_type>(((size - 1) / k_new_align) + 1); return static_cast<void_ptr>(align_trait::allocate(m_alloc, num_aligned)); } @@ constexpr void pop(void_ptr ptr, [[maybe_unused]] std::size_t size) noexcept { LF_ASSUME(size > 0); - size_type num_aligned = safe_cast<size_type>((size + (k_new_align - 1)) / k_new_align); + size_type num_aligned = safe_cast<size_type>(((size - 1) / k_new_align) + 1); align_trait::deallocate(m_alloc, static_cast<alloc_ptr>(ptr), num_aligned); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/batteries/adaptor_stack.cxx` around lines 82 - 94, The rounding computation for num_aligned in push and pop can overflow when evaluating size + (k_new_align - 1); change the calculation to the non-overflowing form ((size - 1) / k_new_align) + 1 (respecting the LF_ASSUME(size > 0) precondition) before passing to safe_cast. Update both occurrences where num_aligned is computed (in push and pop), keep using size_type and safe_cast, and continue to call align_trait::allocate(m_alloc, num_aligned) and align_trait::deallocate(m_alloc, static_cast<alloc_ptr>(ptr), num_aligned) unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/batteries/deque.cxx`:
- Around line 323-324: Add a precondition-checked capacity rounding helper and
use it in the deque constructor to avoid UB from std::bit_ceil and casts:
implement a static constexpr round_capacity(size_type cap) -> diff_type that
computes max_rounded_cap =
std::bit_floor(safe_cast<size_type>(std::numeric_limits<diff_type>::max())),
asserts LF_ASSUME(cap > 0 && cap <= max_rounded_cap), and returns
safe_cast<diff_type>(std::bit_ceil(cap)); then replace the direct
std::bit_ceil(cap) call in the deque(size_type cap, Allocator const &alloc)
constructor with round_capacity(cap).
---
Outside diff comments:
In `@src/batteries/adaptor_stack.cxx`:
- Around line 82-94: The rounding computation for num_aligned in push and pop
can overflow when evaluating size + (k_new_align - 1); change the calculation to
the non-overflowing form ((size - 1) / k_new_align) + 1 (respecting the
LF_ASSUME(size > 0) precondition) before passing to safe_cast. Update both
occurrences where num_aligned is computed (in push and pop), keep using
size_type and safe_cast, and continue to call align_trait::allocate(m_alloc,
num_aligned) and align_trait::deallocate(m_alloc, static_cast<alloc_ptr>(ptr),
num_aligned) unchanged.
In `@src/batteries/geometric_stack.cxx`:
- Around line 331-337: The delete_node function computes allocated_nodes using
signed arithmetic then casts, which can overflow; change the order so you first
cast ptr->size to size_type using safe_cast and then add 1 in the unsigned
domain (i.e., allocated_nodes = safe_cast<size_type>(ptr->size) + 1) before
calling node_traits::destroy and node_traits::deallocate to mirror the
allocation-side ordering and avoid signed overflow.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1a6d25b7-4ab7-4624-8cb8-7e41e34c3fd5
📒 Files selected for processing (4)
CMakePresets.jsonsrc/batteries/adaptor_stack.cxxsrc/batteries/deque.cxxsrc/batteries/geometric_stack.cxx
Summary by CodeRabbit
New Features
Improvements