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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to