Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ func (cache *LRUCache) Get(key interface{}) (value interface{}, ok bool) {
}

func (cache *LRUCache) Put(key interface{}, value interface{}) {
if cache.capacity <= 0 {
// A cache with non-positive capacity holds nothing, so there is nothing
// to store and no eviction to perform.
return
}
n, ok := cache.m[key]
if ok {
cache.remove(n, false)
Expand Down
12 changes: 12 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ func TestLRUCache(t *testing.T) {
testCacheEqual(t, cache, []int{1, 3, 4})
}

func TestLRUCacheNonPositiveCapacity(t *testing.T) {
// A cache created with a non-positive capacity holds nothing and must not
// panic on Put. Previously the eviction path removed cache.tail.prev, which
// on an empty list is the head sentinel whose prev pointer is nil, causing a
// nil pointer dereference.
cache := NewLRUCache(0)
cache.Put("a", 1)
if _, ok := cache.Get("a"); ok {
t.Errorf("NewLRUCache(0): Get(\"a\") ok = true, want false (zero-capacity cache holds nothing)")
}
}

func testEscapeStringLiterals(t *testing.T, input string, expected string) {
t.Helper()
result := EscapeStringLiterals(input)
Expand Down