[SPARK-57520][SQL] Fix UTF8String.codePointFrom and copyUTF8String reading past the end of a truncated trailing UTF-8 sequence#56585
Open
LuciferYang wants to merge 1 commit into
Conversation
…ading past the end of a truncated trailing UTF-8 sequence `codePointFrom` read the declared number of continuation bytes (from `numBytesForFirstByte`) without checking they exist, and `copyUTF8String` copied `end - start + 1` bytes without clamping to what remains. When a string ends in a truncated multi-byte sequence (a leader byte whose width exceeds the remaining bytes), both read past the end of the backing memory. `trimLeft`/`trimRight` build their search character through `copyUTF8String`, so they over-read too. `codePointFrom` now reads continuation bytes through a helper that returns 0 past the end, and `copyUTF8String` clamps the copy length to the bytes that remain. Once `copyUTF8String` stops over-reading, `trimRight` needs a matching accounting fix: it decremented `trimEnd` by the declared character width, which overshoots for a truncated trailing character, so it now uses the actual (clamped) byte count, as `trimLeft` already does. Well-formed UTF-8 is unaffected. Follow-up of SPARK-57507.
Contributor
Author
|
cc @cloud-fan |
Member
|
cc @uros-b |
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.
What changes were proposed in this pull request?
UTF8String.codePointFromdecodes a code point by readingnumBytesForFirstByte(leader)continuation bytes, andcopyUTF8Stringcopiesend - start + 1bytes. Neither bounds the read by the bytes that actually remain, so when a string ends in a truncated multi-byte sequence (a leader byte whose declared width exceeds the remaining bytes), both read past the end of the backing memory.trimLeft/trimRightbuild their search character throughcopyUTF8String, so they over-read too.This PR:
codePointFromreads continuation bytes through a smallcontinuationBytehelper that returns 0 once the index passes the end of the string.copyUTF8Stringclamps the copy length tonumBytes - start.copyUTF8Stringstops over-reading,trimRightneeds a matching accounting fix: it advancedtrimEndby the leader's declared width, which overshoots a truncated trailing character, so it now uses the actual (clamped) byte count, astrimLeftalready does.Why are the changes needed?
UTF8Stringcan hold malformed UTF-8 (for example, bytes from binary coercion or truncated input). For a string ending in an incomplete multi-byte sequence, these methods read out of bounds and produced wrong results:codePointFromassembled a code point from adjacent memory, andtrimRightcould drop valid leading characters. Well-formed UTF-8 is unaffected, since a complete sequence never exceeds the remaining bytes.This is a follow-up to SPARK-57507, which fixed the same kind of over-read in
reverse().Does this PR introduce any user-facing change?
Yes, it fixes incorrect results on malformed input. String operations that reach these methods (such as trimming or code-point access) no longer read past the end of a value that ends in a truncated multi-byte sequence; only previously-incorrect results change. Well-formed strings behave exactly as before.
How was this patch tested?
Added cases to
UTF8StringSuite:testCodePointFrom: truncated trailing 2-, 3-, and 4-byte leaders, including a 4-byte leader with only the last continuation byte missing.copyUTF8StringClampsToRemainingBytes: anendone past the last byte, with a non-zero start so the clamp must usenumBytes - start.trimTruncatedTrailingSequence: trimming a truncated trailing leader keeps the valid preceding character.Each uses a sliced backing array with a trailing sentinel byte, so the previous over-read produces a deterministically wrong value; the cases fail on the old code and pass with the fix.
build/sbt 'unsafe/testOnly *UTF8StringSuite'passes (51 tests).Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 4.8)