Skip to content

Issue: LRUCache<T, 0>::insert panics - #29

Merged
emilio merged 1 commit into
servo:mainfrom
q5438722:fix/capacity-zero-assert
Oct 27, 2026
Merged

emilio merged 1 commit into
servo:mainfrom
q5438722:fix/capacity-zero-assert

Conversation

@q5438722

Copy link
Copy Markdown

Summary

LRUCache::<T, 0>::new() and default() succeed, but calling insert() panics with an index-out-of-bounds error.

Reproducer

use uluru::LRUCache;

fn main() {
    let mut cache = LRUCache::<u8, 0>::default();
    cache.insert(42); // panics
}

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

// Reject zero capacity in new():

assert!(N > 0, "Capacity must be at least 1");

This makes invalid configurations fail immediately with a clear error instead of panicking on the first insertion.

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

@emilio emilio left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure, I guess.

@emilio
emilio merged commit fc4a877 into servo:main Oct 27, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants