{"action":"created","issue":{"url":"https://api.github.com/repos/google/guava/issues/2108","labels_url":"https://api.github.com/repos/google/guava/issues/2108/labels{/name}","comments_url":"https://api.github.com/repos/google/guava/issues/2108/comments","events_url":"https://api.github.com/repos/google/guava/issues/2108/events","html_url":"https://github.com/google/guava/issues/2108","id":95127201,"number":2108,"title":"Cache.asMap().size() may return a negative value","user":{"login":"ben-manes","id":378614,"avatar_url":"https://avatars.githubusercontent.com/u/378614?v=3","gravatar_id":"","url":"https://api.github.com/users/ben-manes","html_url":"https://github.com/ben-manes","followers_url":"https://api.github.com/users/ben-manes/followers","following_url":"https://api.github.com/users/ben-manes/following{/other_user}","gists_url":"https://api.github.com/users/ben-manes/gists{/gist_id}","starred_url":"https://api.github.com/users/ben-manes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ben-manes/subscriptions","organizations_url":"https://api.github.com/users/ben-manes/orgs","repos_url":"https://api.github.com/users/ben-manes/repos","events_url":"https://api.github.com/users/ben-manes/events{/privacy}","received_events_url":"https://api.github.com/users/ben-manes/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/google/guava/labels/package:%20cache","name":"package: cache","color":"207de5"},{"url":"https://api.github.com/repos/google/guava/labels/status:%20accepted","name":"status: accepted","color":"bfe5bf"},{"url":"https://api.github.com/repos/google/guava/labels/type:%20defect","name":"type: defect","color":"e11d21"}],"state":"open","locked":false,"assignee":{"login":"lowasser","id":544569,"avatar_url":"https://avatars.githubusercontent.com/u/544569?v=3","gravatar_id":"","url":"https://api.github.com/users/lowasser","html_url":"https://github.com/lowasser","followers_url":"https://api.github.com/users/lowasser/followers","following_url":"https://api.github.com/users/lowasser/following{/other_user}","gists_url":"https://api.github.com/users/lowasser/gists{/gist_id}","starred_url":"https://api.github.com/users/lowasser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lowasser/subscriptions","organizations_url":"https://api.github.com/users/lowasser/orgs","repos_url":"https://api.github.com/users/lowasser/repos","events_url":"https://api.github.com/users/lowasser/events{/privacy}","received_events_url":"https://api.github.com/users/lowasser/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/google/guava/milestones/14","html_url":"https://github.com/google/guava/milestones/19.0","labels_url":"https://api.github.com/repos/google/guava/milestones/14/labels","id":849127,"number":14,"title":"19.0","description":"","creator":{"login":"cgdecker","id":101568,"avatar_url":"https://avatars.githubusercontent.com/u/101568?v=3","gravatar_id":"","url":"https://api.github.com/users/cgdecker","html_url":"https://github.com/cgdecker","followers_url":"https://api.github.com/users/cgdecker/followers","following_url":"https://api.github.com/users/cgdecker/following{/other_user}","gists_url":"https://api.github.com/users/cgdecker/gists{/gist_id}","starred_url":"https://api.github.com/users/cgdecker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cgdecker/subscriptions","organizations_url":"https://api.github.com/users/cgdecker/orgs","repos_url":"https://api.github.com/users/cgdecker/repos","events_url":"https://api.github.com/users/cgdecker/events{/privacy}","received_events_url":"https://api.github.com/users/cgdecker/received_events","type":"User","site_admin":false},"open_issues":2,"closed_issues":35,"state":"open","created_at":"2014-11-01T03:47:25Z","updated_at":"2015-07-30T18:23:24Z","due_on":null,"closed_at":null},"comments":4,"created_at":"2015-07-15T07:40:36Z","updated_at":"2015-07-30T18:38:53Z","closed_at":null,"body":"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,\r\n```java\r\n(cache, key) -> cache.asMap().keySet().toArray(new Object[cache.asMap().size()]),\r\n(cache, key) -> cache.asMap().values().toArray(new Object[cache.asMap().size()]),\r\n(cache, key) -> cache.asMap().entrySet().toArray(new Entry[cache.asMap().size()]),\r\n```\r\n\r\nThis 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.\r\n\r\n```\r\njava.lang.NegativeArraySizeException\r\nat com.github.benmanes.caffeine.cache.MultiThreadedTest.lambda$new$280(MultiThreadedTest.java:142)\r\nat com.github.benmanes.caffeine.cache.MultiThreadedTest$$Lambda$29/584234975.accept(Unknown Source)\r\nat com.github.benmanes.caffeine.testing.Threads$Thrasher.run(Threads.java:149)\r\nat java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)\r\nat com.github.benmanes.caffeine.testing.ConcurrentTestHarness.lambda$timeTasks$344(ConcurrentTestHarness.java:100)\r\nat com.github.benmanes.caffeine.testing.ConcurrentTestHarness$$Lambda$61/1027825150.run(Unknown Source)\r\nat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)\r\nat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)\r\nat java.lang.Thread.run(Thread.java:745)\r\n```"},"comment":{"url":"https://api.github.com/repos/google/guava/issues/comments/126427964","html_url":"https://github.com/google/guava/issues/2108#issuecomment-126427964","issue_url":"https://api.github.com/repos/google/guava/issues/2108","id":126427964,"user":{"login":"ben-manes","id":378614,"avatar_url":"https://avatars.githubusercontent.com/u/378614?v=3","gravatar_id":"","url":"https://api.github.com/users/ben-manes","html_url":"https://github.com/ben-manes","followers_url":"https://api.github.com/users/ben-manes/followers","following_url":"https://api.github.com/users/ben-manes/following{/other_user}","gists_url":"https://api.github.com/users/ben-manes/gists{/gist_id}","starred_url":"https://api.github.com/users/ben-manes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ben-manes/subscriptions","organizations_url":"https://api.github.com/users/ben-manes/orgs","repos_url":"https://api.github.com/users/ben-manes/repos","events_url":"https://api.github.com/users/ben-manes/events{/privacy}","received_events_url":"https://api.github.com/users/ben-manes/received_events","type":"User","site_admin":false},"created_at":"2015-07-30T18:38:53Z","updated_at":"2015-07-30T18:38:53Z","body":"Yes, its that simple. In `longSize()` use `return Math.max(0, sum)` which will cover all of the size() methods. This is essentially what I do in my [fixture](https://github.com/ben-manes/caffeine/blob/master/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/GuavaCacheFromContext.java#L207), which is used to run all my tests against both Caffeine and Guava implementations to find bugs / check compatibility.\r\n\r\nAgain I'm not sure why this occurs from reading the code, but I was able to reproduce the exception (its racy so takes a few tries). The fix is safe and non-invasive, so easier to resolve it there than dig into the race causing it."}}