[SPARK-57578][SQL] Fix UTF8String.codePointFrom/trimLeft/trimRight out-of-bounds read on truncated trailing UTF-8 sequences#56625
Open
jubins wants to merge 2 commits into
Open
Conversation
…t-of-bounds read on truncated trailing UTF-8 sequences
uros-b
reviewed
Jun 21, 2026
uros-b
left a comment
Member
There was a problem hiding this comment.
I think that @LuciferYang already has an ongoing PR for this issue.
@jubins please take a look at: #56585.
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 is the purpose of the change
Fixes SPARK-57578 —
UTF8String.codePointFrom,trimLeft(UTF8String), andtrimRight(UTF8String)perform out-of-bounds native memory reads when the string ends in a truncated multi-byte UTF-8 sequence (i.e., a leading byte is present but one or more continuation bytes are missing).The root cause is that all three methods call
numBytesForFirstByte()to determine a character's byte width and then unconditionally read that many bytes forward, without checking whether the range stays withinnumBytes. For a string whose last byte is a lone 2/3/4-byte leader, this reads past the end of the backing buffer viaPlatform.getByte(base, offset + n)— which either silently returns adjacent memory or causes a JVM crash depending on the allocator and platform.The companion method
reverse()was fixed for the exact same class of bug in SPARK-57507 (line 1160) usingMath.min(numBytesForFirstByte(getByte(i)), numBytes - i). That commit explicitly notedcodePointFrom,trimLeft, andtrimRightas unfixed siblings tracked in SPARK-57520. This PR applies the same clamping pattern to all three.Brief change log
UTF8String.codePointFrom(): replacedint numBytes = numBytesForFirstByte(b)withint numBytes = Math.min(numBytesForFirstByte(b), this.numBytes - byteIndex)so thecase 2/3/4branches never read continuation bytes that do not existUTF8String.trimLeft(UTF8String): clamped thecopyUTF8Stringend argument fromsearchIdx + numBytesForFirstByte(...) - 1tosearchIdx + Math.min(numBytesForFirstByte(...), numBytes - searchIdx) - 1UTF8String.trimRight(UTF8String): clampedstringCharLen[numChars]fromnumBytesForFirstByte(getByte(charIdx))toMath.min(numBytesForFirstByte(getByte(charIdx)), numBytes - charIdx)so the subsequentcopyUTF8Stringcall cannot extend past the bufferUTF8StringSuite: added unit tests for all three methods covering lone 2-, 3-, and 4-byte leaders as both the source string and the trim-set stringVerifying this change
This change is covered by unit tests in
UTF8StringSuite:trimRightWithTrimString/trimLeftWithTrimString: new assertions confirm that passing a truncated single-byte trim-set (lone0xC3,0xE4,0xF0) does not crash and returns the input unchanged when no match exists; also covers a source string whose trailing byte is a lone multi-byte leadertestCodePointFrom: new assertions confirm that callingcodePointFrom(0)on a 1-byte string whose only byte is a 2-byte leader, andcodePointFrom(1)on a 2-byte string whose second byte is a 3- or 4-byte leader, completes without reading out of boundsDoes this pull request potentially affect one of the following parts
@Public/@Evolving: no —UTF8Stringis an internal unsafe typetrimLeft,trimRight, andcodePointFromare hot paths, but the fix adds only a singleMath.mincall per character iteration, which is negligibleDocumentation
Does this pull request introduce a new feature? no
If yes, how is the feature documented? not applicable
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Opus 4.8