JDK-8222930 : ConcurrentSkipListMap.clone() shares size variable between original and clone
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.concurrent
  • Affected Version: 10,11,12,13
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2019-04-24
  • Updated: 2020-04-08
  • Resolved: 2019-05-02
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 12 JDK 13
11.0.4Fixed 12.0.2Fixed 13 b20Fixed
Description
A bug has been found where cloning a ConcurrentSkipListMap doubles the "size" return value in both original and clone, as a result of the two maps sharing the same size variable (which also causes other problems).

The contents of the maps themselves does not appear to be affected. Clone a map of size 3 and you still have 3 elements in each map post-clone, even though the size() method is not returning 6 for both.

It seems this was caused by the creation of the "adder" variable introduced in JDK-8186226, for JDK10, to track the array size. Since adder is an Object, a shallow clone results in two objects sharing the same variable.

Since the clone process already wipes other variables, it seems a good idea to do the same here. 

Code fix and test available.

Webrev for jdk/jdk : http://cr.openjdk.java.net/~afarley/8222930.0/jdk13/webrev/
Comments
Martin's test looks good to me. As a nitpick, I tweaked the "for" loop to make it look less like a lambda. No other changes. http://cr.openjdk.java.net/~afarley/8222930.1/jdk13/webrev
25-04-2019

Proposed fix with slightly more general testing: https://cr.openjdk.java.net/~martin/webrevs/jdk/jsr166-integration/ConcurrentSkipListMap.clone/index.html
24-04-2019

No problem. I've put out a call to the list for reviews and sponsor. https://mail.openjdk.java.net/pipermail/core-libs-dev/2019-April/059848.html Lets get this thing fixed. :)
24-04-2019

Thanks for finding this! Obviously, the adder field should have nulled out.
24-04-2019

Once the form of the fix has been agreed, I propose we backport this, as it affects 10, 11, and 12, as well as head (13).
24-04-2019