Repository navigation
Fix two out-of-bounds reads in ScriptScanner - #3
Merged
Merged
Conversation
ScriptScanner walks the input buffer with `next_byte_` and `byte_length_`, but two paths read one byte past `byte_length_`: 1. `GetOneTextSpan` — after the main `while (take < byte_length_)` loop exits, the back-up-to-character-boundary loop reads `next_byte_[take]` without checking `take < byte_length_`. The matching loop at the end of `GetOneScriptSpan` already includes that guard; mirror it here. 2. `GetOneScriptSpan` — at the script-change check, after consuming a `tlen`-byte UTF-8 character, the lookahead `GetUTF8LetterScriptNum(next_byte_ + take + tlen)` is unconditional. When the consumed character ends exactly at `byte_length_`, `GetUTF8LetterScriptNum` -> `UTF8OneCharLen` dereferences the byte one past the buffer end. Skip the lookahead when no following byte is available; semantically equivalent to "no next character to compare". Both reads are flagged by MemorySanitizer when the input is followed by uninitialized bytes (e.g. trailing pad bytes in a caller's buffer).
Merged
1 task
leshikus
pushed a commit
to leshikus/ClickHouse
that referenced
this pull request
May 7, 2026
`CLD2::ScriptScanner` had two paths that read one byte past the input buffer when a consumed UTF-8 character ended exactly at `byte_length_`. With a `ColumnString` whose trailing padding bytes are uninitialized, this tripped MSan in `detectLanguageMixed` (and the other `detectLanguage*` functions) and showed up as flaky failures of the `function_prop_fuzzer` unit test on master. Patched in `ClickHouse/cld2#3`. Add a stateless test that exercises the boundary, including the inputs reported on the issue. Closes ClickHouse#103765 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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.
ScriptScannerwalks the input buffer withnext_byte_andbyte_length_, but two paths read one byte pastbyte_length_:GetOneTextSpan— after the mainwhile (take < byte_length_)loop exits, the back-up-to-character-boundary loop readsnext_byte_[take]without checkingtake < byte_length_. The matching loop at the end ofGetOneScriptSpanalready includes that guard; mirror it here.GetOneScriptSpan— at the script-change check, after consuming atlen-byte UTF-8 character, the lookaheadGetUTF8LetterScriptNum(next_byte_ + take + tlen)is unconditional. When the consumed character ends exactly atbyte_length_,GetUTF8LetterScriptNum->UTF8OneCharLendereferences the byte one past the buffer end. Skip the lookahead when no following byte is available; semantically equivalent to "no next character to compare".Both reads are flagged by MemorySanitizer when the input is followed by uninitialized bytes (e.g. trailing pad bytes in a caller's buffer).
Reproducer (via ClickHouse, MSan build)
Tracking issue on the caller side: ClickHouse/ClickHouse#103765