Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 363749

Summary: Remove recursion from IdentityWeakHashMap$WeakEntry.clone()
Product: z_Archived Reporter: David Minsky <david.minsky>
Component: EclipselinkAssignee: David Minsky <david.minsky>
Status: RESOLVED FIXED QA Contact: Project Inbox <eclipselink.foundation-inbox>
Severity: normal    
Priority: P3    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Fix none

Description David Minsky CLA 2011-11-14 15:54:43 EST
Created attachment 206989 [details]
Fix

Current code for WeakEntry.clone() (inner class in IdentityWeakHashMap) is recursive:

protected Object clone(ReferenceQueue refQueue) {
    return new WeakEntry<K,V>(hash, key.get(), value.get(), ((next == null) ? null : (WeakEntry<K,V>)next.clone(refQueue)), refQueue);
}

Recursive cloning in the product can potentially cause a stack overflow with a large set of data and a normal stack size configuration. Ideally, recursion should not be used when cloning.

Propoing to change the code to:

protected Object clone(ReferenceQueue refQueue) {
    WeakEntry current = this;
    WeakEntry root = new WeakEntry(current.hash, current.key.get(), current.value.get(), null, refQueue);
    WeakEntry currentClone = root;

    while (current.next != null) {
        currentClone.next = new WeakEntry(current.next.hash, current.next.key.get(), current.next.value.get(), null, refQueue);
        current = current.next;
        currentClone = currentClone.next;
    }

    return root;
}
Comment 1 David Minsky CLA 2011-11-15 10:45:10 EST
As an addition, this is related to the work done in:

Bug 237812 - RECURSION IN IDENTITYHASHSET.CLONE() CAN CAUSE STACK OVERFLOW WITH LARGE SET
Comment 2 David Minsky CLA 2011-11-15 10:50:29 EST
Fixed in trunk (2.4) at revision: 10395
Comment 3 Eclipse Webmaster CLA 2022-06-09 10:10:36 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink