This repository was archived by the owner on Apr 23, 2026. It is now read-only.
forked from Signal65/elasticsearch-Copilot
-
Notifications
You must be signed in to change notification settings - Fork 1
Optimized text for full unicode and some escape sequences (#129169) #7
Open
MitchLewis930
wants to merge
1
commit into
pr_017_before
Choose a base branch
from
pr_017_after
base: pr_017_before
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−43
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,9 +21,14 @@ | |
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class ESUTF8StreamJsonParser extends UTF8StreamJsonParser { | ||
| protected int stringEnd = -1; | ||
| protected int stringLength; | ||
|
|
||
| private final List<Integer> backslashes = new ArrayList<>(); | ||
|
|
||
| public ESUTF8StreamJsonParser( | ||
| IOContext ctxt, | ||
|
|
@@ -43,15 +48,12 @@ public ESUTF8StreamJsonParser( | |
| /** | ||
| * Method that will try to get underlying UTF-8 encoded bytes of the current string token. | ||
| * This is only a best-effort attempt; if there is some reason the bytes cannot be retrieved, this method will return null. | ||
| * Currently, this is only implemented for ascii-only strings that do not contain escaped characters. | ||
| */ | ||
| public Text getValueAsText() throws IOException { | ||
| if (_currToken == JsonToken.VALUE_STRING && _tokenIncomplete) { | ||
| if (stringEnd > 0) { | ||
| final int len = stringEnd - 1 - _inputPtr; | ||
| // For now, we can use `len` for `stringLength` because we only support ascii-encoded unescaped strings, | ||
| // which means each character uses exactly 1 byte. | ||
| return new Text(new XContentString.UTF8Bytes(_inputBuffer, _inputPtr, len), len); | ||
| return new Text(new XContentString.UTF8Bytes(_inputBuffer, _inputPtr, len), stringLength); | ||
| } | ||
| return _finishAndReturnText(); | ||
| } | ||
|
|
@@ -69,21 +71,71 @@ protected Text _finishAndReturnText() throws IOException { | |
| final int[] codes = INPUT_CODES_UTF8; | ||
| final int max = _inputEnd; | ||
| final byte[] inputBuffer = _inputBuffer; | ||
| while (ptr < max) { | ||
| stringLength = 0; | ||
| backslashes.clear(); | ||
|
|
||
| loop: while (true) { | ||
| if (ptr >= max) { | ||
| return null; | ||
| } | ||
| int c = inputBuffer[ptr] & 0xFF; | ||
| if (codes[c] != 0) { | ||
| if (c == INT_QUOTE) { | ||
| stringEnd = ptr + 1; | ||
| final int len = ptr - startPtr; | ||
| // For now, we can use `len` for `stringLength` because we only support ascii-encoded unescaped strings, | ||
| // which means each character uses exactly 1 byte. | ||
| return new Text(new XContentString.UTF8Bytes(inputBuffer, startPtr, len), len); | ||
| switch (codes[c]) { | ||
| case 0 -> { | ||
| ++ptr; | ||
| ++stringLength; | ||
| } | ||
| case 1 -> { | ||
| if (c == INT_QUOTE) { | ||
| // End of the string | ||
| break loop; | ||
| } | ||
| assert c == INT_BACKSLASH; | ||
| backslashes.add(ptr); | ||
| ++ptr; | ||
| if (ptr >= max) { | ||
| // Backslash at end of file | ||
| return null; | ||
| } | ||
| c = inputBuffer[ptr] & 0xFF; | ||
| if (c == '"' || c == '/' || c == '\\') { | ||
| ptr += 1; | ||
| stringLength += 1; | ||
| } else { | ||
| // Any other escaped sequence requires replacing the sequence with | ||
| // a new character, which we don't support in the optimized path | ||
| return null; | ||
| } | ||
| } | ||
| case 2, 3, 4 -> { | ||
| int bytesToSkip = codes[c]; | ||
| if (ptr + bytesToSkip > max) { | ||
| return null; | ||
| } | ||
| ptr += bytesToSkip; | ||
| ++stringLength; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Supplementary characters cause stringLength mismatch with String.length()Medium Severity The parser increments |
||
| default -> { | ||
| return null; | ||
| } | ||
| return null; | ||
| } | ||
| ++ptr; | ||
| } | ||
| return null; | ||
|
|
||
| stringEnd = ptr + 1; | ||
| if (backslashes.isEmpty()) { | ||
| return new Text(new XContentString.UTF8Bytes(inputBuffer, startPtr, ptr - startPtr), stringLength); | ||
| } else { | ||
| byte[] buff = new byte[ptr - startPtr - backslashes.size()]; | ||
| int copyPtr = startPtr; | ||
| int destPtr = 0; | ||
| for (Integer backslash : backslashes) { | ||
| int length = backslash - copyPtr; | ||
| System.arraycopy(inputBuffer, copyPtr, buff, destPtr, length); | ||
| destPtr += length; | ||
| copyPtr = backslash + 1; | ||
| } | ||
| System.arraycopy(inputBuffer, copyPtr, buff, destPtr, ptr - copyPtr); | ||
| return new Text(new XContentString.UTF8Bytes(buff), stringLength); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
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
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.


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Second call to
getValueAsTextreturns raw bytes with escapesMedium Severity
When
getValueAsText()is called a second time on a string containing escape sequences (\",\\,\/), the cached path at line 54-56 returns raw bytes from_inputBufferinstead of the processed buffer with backslashes removed. The first call creates a new buffer via_finishAndReturnText(), but subsequent calls bypass this and return unprocessed data. This also causes a mismatch betweenbytes.length()andstringLength, triggering an assertion failure inText.string().