Repository navigation
Issue: LRUCache<T, 0>::insert panics - #29
Merged
Merged
Conversation
A capacity-0 cache could be constructed, but the first insert always panicked: ArrayVec<_, 0> is trivially full, so insert takes the eviction path and pop_back dereferences entries[0] on an empty array (index out of bounds: len 0 but index 0, src/lib.rs:230). Fail fast at construction instead, symmetric with the existing u16::MAX upper-bound assert. Since new() is const, const contexts now fail at compile time. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 3f5d733b-593a-484a-8f64-ea354a3c7d36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LRUCache::<T, 0>::new() and default() succeed, but calling insert() panics with an index-out-of-bounds error.
Reproducer
Root Cause
new() checks only the upper capacity bound (N < u16::MAX) and allows N = 0. Since a zero-capacity cache is always full, insert() attempts eviction from an empty array, causing an out-of-bounds panic.
Suggested Fix
This makes invalid configurations fail immediately with a clear error instead of panicking on the first insertion.