You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@casbin.apache.org by "nikolauspschuetz (via GitHub)" <gi...@apache.org> on 2026/08/13 19:39:01 UTC
[PR] fix: guard LRUCache against non-positive capacity [casbin]
nikolauspschuetz opened a new pull request, #1749:
URL: https://github.com/apache/casbin/pull/1749
### What
`NewLRUCache` and `NewSyncLRUCache` are exported public API, but calling `Put` on a cache created with a non-positive capacity panics with a nil pointer dereference.
Reproduce:
```go
c := util.NewLRUCache(0)
c.Put("a", 1) // panic: runtime error: invalid memory address or nil pointer dereference
```
On the first insert, the eviction branch is taken (`len(cache.m) >= cache.capacity` is `0 >= 0`), which calls `cache.remove(cache.tail.prev, false)`. On an empty list `cache.tail.prev` is the `head` sentinel, whose `prev` is `nil`, so `remove` dereferences `nil` at `util/util.go`.
### Fix
Return early from `Put` when `capacity <= 0` — such a cache holds nothing, so there is nothing to store and no eviction to perform. This keeps `Get` returning `(nil, false)` for every key, consistent with a zero-capacity cache.
Guarding the eviction path instead would let a zero-capacity cache incorrectly hold one entry, so the early return is the more correct minimal fix.
### Test
Added `TestLRUCacheNonPositiveCapacity` in `util/util_test.go`. It panics on `master` and passes with this change:
- before: `panic: runtime error: invalid memory address or nil pointer dereference` (in `Put`)
- after: `ok github.com/casbin/casbin/v3/util`
---
for the record: fix found and drafted with AI assistance; reviewed and verified by me.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org
Re: [PR] fix: guard LRUCache against non-positive capacity [casbin]
Posted by "hsluoyz (via GitHub)" <gi...@apache.org>.
hsluoyz merged PR #1749:
URL: https://github.com/apache/casbin/pull/1749
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org