Skip to content

Fix two out-of-bounds reads in ScriptScanner - #3

Merged
Algunenano merged 1 commit into
masterfrom
fix-utf8-oob-reads
May 6, 2026
Merged

Algunenano merged 1 commit into
masterfrom
fix-utf8-oob-reads

Conversation

@Algunenano

Copy link
Copy Markdown
Member

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).

Reproducer (via ClickHouse, MSan build)

SET allow_experimental_nlp_functions = 1;
SELECT detectLanguageMixed(materialize('abcdα'));
==1125391==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 CLD2::UTF8OneCharLen(char const*) contrib/cld2/internal/utf8statetable.h:270:10
    #1 CLD2::GetUTF8LetterScriptNum(char const*) contrib/cld2/internal/getonescriptspan.cc:1084:16
    #2 CLD2::ScriptScanner::GetOneScriptSpan(CLD2::LangSpan*) contrib/cld2/internal/getonescriptspan.cc:922:21
    ...

Tracking issue on the caller side: ClickHouse/ClickHouse#103765

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).
@Algunenano
Algunenano merged commit b19c9d5 into master May 6, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant