Fix MutableJsonIndexImpl corrupting doc ID mapping on invalid UTF-16 strings#18738
Open
spapin wants to merge 1 commit into
Open
Fix MutableJsonIndexImpl corrupting doc ID mapping on invalid UTF-16 strings#18738spapin wants to merge 1 commit into
spapin wants to merge 1 commit into
Conversation
… surrogates Utf8.encodedLength throws on unpaired surrogates, aborting the posting-list build loop after _docIdMapping has already been extended. This leaves the two counters permanently out of sync, silently shifting all subsequent documents' json_match results by one. _bytesSize is only a memory heuristic, so a size calculation must never abort indexing. Wraps the calls in estimateLength(), which falls back to String.getBytes(UTF_8) — which never throws — for malformed strings. Fixes apache#18737
spapin
commented
Jun 15, 2026
| Preconditions.checkState(_nextFlattenedDocId + numRecords >= 0, "Got more than %s flattened records", | ||
| Integer.MAX_VALUE); | ||
| for (int i = 0; i < numRecords; i++) { | ||
| _docIdMapping.add(_nextDocId); |
Author
There was a problem hiding this comment.
Here, we add all the records to _docIdMapping.
On line 138, the call to Utf8.encodedLength may crash the function before _postingListMap is also increased. This is the source of the discrepancy between the json index and the mapped docId. Fixing the estimator ensures the issue doesn't happen.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #18738 +/- ##
============================================
- Coverage 64.54% 64.53% -0.01%
Complexity 1305 1305
============================================
Files 3380 3380
Lines 209642 209645 +3
Branches 32776 32776
============================================
- Hits 135304 135300 -4
- Misses 63480 63486 +6
- Partials 10858 10859 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Why
MutableJsonIndexImpl#addextends_docIdMappingfor a document's flattened records, then builds the posting lists and increments_nextFlattenedDocId. While building those posting lists it maintains a running_bytesSize(a memory heuristic used to cap the mutable index) via Guava'sUtf8.encodedLength(...).Utf8.encodedLengththrowsIllegalArgumentExceptionwhen a string contains an unpaired UTF-16 surrogate. Because it's called insidecomputeIfAbsent— after_docIdMappinghas already been extended but before_nextFlattenedDocIdis incremented — a single malformed value aborts the build loop and leaves_docIdMappingand_nextFlattenedDocIdpermanently out of sync._nextDocIdstill advances in thefinallyblock, so every subsequent document'sjson_matchresult is silently shifted by one flattened doc id.This behavior lasts until the segment is flushed. The non-mutable json indices do not estimate size, so its ids are in sync.
Fix
Route the size accounting through an
estimateLengthhelper that falls back toString.getBytes(UTF_8).length(which never throws — malformed input is replaced) whenUtf8.encodedLengththrows._bytesSizeis only a heuristic, so an approximate length for a malformed string is acceptable; aborting indexing is not.Testing
Added
testAddRecordWithUnpairedSurrogateDoesNotShiftDocIds: indexes three records where the middle value is an unpaired surrogate (\uD800) and assertsname='first'→ doc 0 andname='third'→ doc 2. Fails before this change (doc ids shifted), passes after.Fixes #18737