Skip to content

Cache.asMap().size() may return a negative value #2108

@ben-manes

Description

@ben-manes

My multithreaded thrashing tests failed with Guava due to size() returning a negative value. The size is used for verifying that the toArray() methods are threadsafe (this was a common bug until JDK6 rewrote AbstractCollection's to be tolerant to races). The lines in question are,

(cache, key) -> cache.asMap().keySet().toArray(new Object[cache.asMap().size()]),
(cache, key) -> cache.asMap().values().toArray(new Object[cache.asMap().size()]),
(cache, key) -> cache.asMap().entrySet().toArray(new Entry[cache.asMap().size()]),

This results in a NegativeArraySizeException unless worked around using Math.max(0, cache.asMap().size()) which is now done in the guava fixture. I'm not sure why this occurs, and only happened after tweaking some JVM args. Regardless this failure was reproducible and is simple to fix.

java.lang.NegativeArraySizeException
at com.github.benmanes.caffeine.cache.MultiThreadedTest.lambda$new$280(MultiThreadedTest.java:142)
at com.github.benmanes.caffeine.cache.MultiThreadedTest$$Lambda$29/584234975.accept(Unknown Source)
at com.github.benmanes.caffeine.testing.Threads$Thrasher.run(Threads.java:149)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at com.github.benmanes.caffeine.testing.ConcurrentTestHarness.lambda$timeTasks$344(ConcurrentTestHarness.java:100)
at com.github.benmanes.caffeine.testing.ConcurrentTestHarness$$Lambda$61/1027825150.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions