Menu

#13 Potential NPE in NonBlockingHashMap.clone()

open
nobody
None
5
2012-02-15
2012-02-15
Anonymous
No

As on the tin, the following code segment has a race that may result in NPE:

// Now copy sanely
for( TypeK K : keySet() ) {
final TypeV V = get(K); // Do an official 'get'
t.put(K,V);
}

While every key K will be non-null (and a valid key reference at the time extracted from the keyset), there is no guarantee that the mapping will not be removed by some other thread before get(K) can read the underlying array for the value, thus leading to a return of 'null' and this leading to a rapid NPE on the put into the new map. Since it looks as tho this block mimics the behavior of putAll(), and putAll() does not appear to have this same NPE concern, would it not make more sense to use:

t.putAll(this);

?

Discussion


Log in to post a comment.