Repository navigation
Recreate targetMap in LinkedCaseInsensitiveMap copy constructor - #37628
Open
guanchengang wants to merge 1 commit into
Open
guanchengang wants to merge 1 commit into
guanchengang wants to merge 1 commit into
Conversation
Prior to this commit, the copy constructor of `LinkedCaseInsensitiveMap` cloned the `targetMap`, which is an anonymous `LinkedHashMap` subclass holding a synthetic `this$0` reference to the original map instance. Consequently, the cloned copy still delegated `containsKey` and `removeEldestEntry` to the original map, causing case-insensitive key removals to mutate the source map and leaving the copy's caseInsensitiveKeys out of sync. This commit replaces the `clone()` call with a freshly created anonymous `LinkedHashMap` bound to the new instance, then populates it via `putAll()`. The `caseInsensitiveKeys` and `locale` fields are still copied from the source map, preserving the original copy semantics. Signed-off-by: Chengang Guan <guanchengang@qq.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
LinkedCaseInsensitiveMapstores its data in atargetMapthat is created as an anonymousLinkedHashMapsubclass. This anonymous class overridescontainsKey(...)andremoveEldestEntry(...)with closures that refer back to the enclosingLinkedCaseInsensitiveMapinstance via the compiler-generatedthis$0field.The copy constructor, however, does:
Object.clone()is a shallow copy: the clone of the anonymousLinkedHashMapstill holds athis$0reference pointing at the originalLinkedCaseInsensitiveMap, not the newly created copy. As a result, calls routed through the copied targetMap's overrides (containsKey,removeEldestEntry) end up operating on the source instance's state instead of the copy's.The following snippet demonstrates that a copied map cannot be mutated consistently: