Skip to content

Avoid phantom keys in LinkedCaseInsensitiveMap.computeIfAbsent - #37351

Merged
sbrannen merged 1 commit into
spring-projects:7.0.xfrom
junhyeong9812:fix/lcim-computeifabsent-phantom-key
Sep 30, 2026
Merged

sbrannen merged 1 commit into
spring-projects:7.0.xfrom
junhyeong9812:fix/lcim-computeifabsent-phantom-key

Conversation

@junhyeong9812

Copy link
Copy Markdown
Contributor

Overview

LinkedCaseInsensitiveMap.computeIfAbsent registers the case-insensitive key before the mapping function runs. When the function returns null or throws, Map#computeIfAbsent records no mapping, but the registration stays behind. The map then reports a key it does not contain: containsKey(key) is true while size() is 0, keySet() is empty and get(key) returns null. This PR registers the key only once a value has actually been computed.

Problem

LinkedCaseInsensitiveMap keeps two structures in sync: targetMap, which holds the entries under their original casing, and caseInsensitiveKeys, which maps each converted key to the stored key. containsKey consults the latter, while size, keySet and get go through the former.

computeIfAbsent called caseInsensitiveKeys.putIfAbsent(convertKey(key), key) up front and then delegated to targetMap.computeIfAbsent(key, mappingFunction). Per the Map#computeIfAbsent contract, a null result or an exception from the mapping function records no mapping, so the two structures disagree afterwards:

LinkedCaseInsensitiveMap<String> map = new LinkedCaseInsensitiveMap<>();
map.computeIfAbsent("Key", key -> null);
map.containsKey("key"); // true
map.size();             // 0
map.keySet();           // []

The stale registration also leaks into later insertions: map.computeIfAbsent("KEY", key -> "value") then finds the registered key "Key", takes the existing-key branch (introduced for gh-26868) and stores the value under "Key" rather than the caller's "KEY".

The in-tree callers (StompHeaders, NativeMessageHeaderAccessor, MockHttpServletResponse) always return a non-null value, so this surfaces with user code that uses computeIfAbsent as a lookup cache, where a null result or an exception is an expected outcome.

Fix

computeIfAbsent now only looks up the case-insensitive key up front. The existing-key path is unchanged in behavior: an existing non-null value is returned as is, and an existing null value is computed under the stored casing. For a new key, the mapping function is wrapped so that the case-insensitive key is registered right after a non-null value has been computed, before targetMap inserts the entry. A null result or an exception therefore leaves both structures untouched.

Registering before the insertion (rather than after targetMap.computeIfAbsent returns) matters for subclasses overriding removeEldestEntry: LinkedHashMap invokes it during the insertion, and the internal hook removes the evicted key's registration. Registering afterwards would re-add the registration for an entry that was evicted within the same call; with the registration in place beforehand, eviction cleans it up the same way it does for put.

New tests cover a null result, a throwing mapping function, the casing of a subsequent insertion, an existing null-valued entry with a null result, and an entry evicted immediately by removeEldestEntry. The full spring-core test suite passes.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 30, 2026
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Sep 30, 2026
@sbrannen sbrannen self-assigned this Sep 30, 2026
@sbrannen sbrannen added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 30, 2026
@sbrannen sbrannen added this to the 7.0.10 milestone Sep 30, 2026
computeIfAbsent registered the case-insensitive key before invoking the
mapping function. If the function returned null or threw an exception,
no mapping was recorded in the target map, but the key registration was
left behind. The map then reported containsKey(key) as true while size()
was 0, keySet() was empty, and get(key) returned null. A later insertion
with a different casing also reused the stale casing of the failed call,
since the existing-key branch resolved to the registered key.

The case-insensitive key is now only looked up up front. For a new key,
it is registered from within the mapping function once a non-null value
has been computed, which is still before the entry is inserted. A null
result or an exception therefore leaves both maps untouched, while a
removeEldestEntry override that evicts the new entry right away still
removes the registration, as it does for put. The existing-key path
keeps computing under the stored casing.

Closes spring-projectsgh-37351

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
@sbrannen
sbrannen force-pushed the fix/lcim-computeifabsent-phantom-key branch from 95a1db9 to b48e801 Compare September 30, 2026 10:45
@sbrannen
sbrannen changed the base branch from main to 7.0.x September 30, 2026 10:45
@sbrannen
sbrannen merged commit 1b4955e into spring-projects:7.0.x Sep 30, 2026
3 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

Fixed via 1b4955e

@sbrannen

Copy link
Copy Markdown
Member

This has been merged into 7.0.x and main.

Thanks

@junhyeong9812

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing and merging this, and for the backport to 7.0.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants