Diptonil / raging-coffee

I had too much coffee and decided to mirror my actions by writing too many programs in Java.
MIT License
19 stars 4 forks source link

[BUG] Possible IndexOutOfBounds Exception in RandomReplacementCache Logic #16

Closed jivjen closed 1 year ago

jivjen commented 1 year ago

Describe the bug Currently, in RandomReplacementCache the below code is being used to randomly remove elements from the cache cache.remove(random.nextInt(CACHE_SIZE + 1));

The random.nextInt function here can end up generating 5 sometimes whereas the collection has the size=5 which would mean the indices are only from 0-4. This would then end up in an IndexOutOfBoundsException.

Expected behavior IndexOutOfBounds Exception should not be seen

Proposed solution The nextInt function should just be passed with CACHE_SIZE and not CACHE_SIZE+1

Please assign the issue to me. Will push the fix for this.

Diptonil commented 1 year ago

Thank you for pointing this out! Do combine the fix for this as well in your previous PR.