Skip to content

Fix sanitizer findings exposed by function_prop_fuzzer - #103491

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-function-prop-fuzzer-sanitizer-findings
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-function-prop-fuzzer-sanitizer-findings

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Summary

Unit tests (msan, function_prop_fuzzer) and Unit tests (asan_ubsan, function_prop_fuzzer) have been reporting three distinct sanitizer findings on master for the past week. CIDB over the last 3 days shows 12 master hits across all three signatures, with 50+ PRs polluted per day as the same bugs surface on unrelated changes:

Signature Fix
UBSan src/Parsers/Lexer.cpp:119:43 — applying non-zero offset 262144 to null pointer Guard begin + max_query_size with begin != nullptr
MSan src/Functions/URL/protocol.h:17:9 in getURLScheme (via protocol(...)) Short-circuit size == 0 before dereferencing *pos
MSan src/Common/UTF8Helpers.cpp:156:10 in Poco::UTF8Encoding::queryConvert (propagated from UTF8::convertUTF8ToCodePoint) Return empty for zero-length input at convertUTF8ToCodePoint — root-cause fix that also covers the Volnitsky.h putNGramBase MSan signature and every other caller (HasSubsequenceImpl.h, StringSearcher.h, translate.cpp, initcapUTF8.cpp, FunctionsStringDistance.cpp)

In production the protocol.h and convertUTF8ToCodePoint reads are typically covered by ColumnString padding, so end users are not affected. The function property fuzzer drives these functions with unpadded buffers, which is where the sanitizers fire.

An earlier AI-authored attempt at these fixes was opened in #103483 and closed by the author the same day (cleaning AIs mess). The parent PR #99740 contains the getURLScheme guard but has been blocked on unrelated libFuzzer CI work since March, so the trunk bugs keep accumulating. This PR re-applies the three fixes under a signed-off commit, adds regression tests, and fixes the MSan queryConvert signature at its root rather than guarding individual callers.

Reproduction and validation

The Lexer null-begin bug is deterministically reproducible under an ASan+UBSan build:

$ build-asan/src/unit_tests_dbms --gtest_filter='LexerTest.NextTokenWithNullBeginAndMaxQuerySize'
/home/ubuntu/nerve-workspace/ClickHouse/src/Parsers/Lexer.cpp:119:43: runtime error: applying non-zero offset 262144 to null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/Parsers/Lexer.cpp:119:43

With the fix applied, all three LexerTest cases pass. The SQL regression test 04113_sanitizer_empty_string_ops passes against the ASan binary.

Matching CIDB master hits this week:

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix sanitizer findings in function_prop_fuzzer unit-test job: null-pointer arithmetic in Lexer::nextToken, reading past empty-string buffer in getURLScheme, and zero-length Poco::UTF8Encoding::queryConvert call from UTF8::convertUTF8ToCodePoint.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Three related edge-case bugs surface as P0 sanitizer reports on the
`Unit tests (msan|asan_ubsan, function_prop_fuzzer)` master job
(originally from the libFuzzer-driven function property fuzzer):

1. `src/Parsers/Lexer.cpp:119` — `Lexer::nextToken` computes
   `begin + max_query_size` unconditionally. When the fuzzer constructs
   a `Lexer` with `begin == nullptr` and a non-zero `max_query_size`,
   UBSan reports:
       applying non-zero offset 262144 to null pointer
   Guard the arithmetic with a `begin != nullptr` check.

2. `src/Functions/URL/protocol.h:17` — `getURLScheme` dereferences
   `*pos` before checking `size`. With `size == 0` the read goes
   one byte past the buffer. In production `ColumnString` padding
   hides this, but the fuzzer drives the function with unpadded
   buffers and MSan flags a use-of-uninitialized-value. Short-circuit
   the empty-input case before touching `*pos`.

3. `src/Common/UTF8Helpers.cpp` — `UTF8::convertUTF8ToCodePoint`
   forwards straight to `Poco::UTF8Encoding::queryConvert`, which
   reads the first byte without length-check, yielding
       MSan: use-of-uninitialized-value at UTF8Encoding.cpp:156:10
   Callers naturally pass `end - pos` which can be zero at string
   boundaries (`HasSubsequenceImpl.h`, `Volnitsky.h`, `StringSearcher.h`,
   `translate.cpp`, `initcapUTF8.cpp`, `FunctionsStringDistance.cpp`).
   Fix the root cause at `convertUTF8ToCodePoint` by returning empty
   for zero-length input; this also eliminates the
   `Volnitsky.h:724/475 putNGramBase` MSan signature for free.

Regression tests:
  - `src/Parsers/tests/gtest_Lexer.cpp` — deterministic UBSan reproducer
    for the null-begin case, plus sanity coverage for a real query and
    for an empty-but-non-null buffer.
  - `tests/queries/0_stateless/04113_sanitizer_empty_string_ops.sql`
    — exercises `protocol('')` and `hasSubsequenceCaseInsensitiveUTF8`
    with empty haystack/needle combinations (both ASCII and UTF-8).

The Lexer repro was validated locally against an ASan+UBSan build:
without the fix, running `LexerTest.NextTokenWithNullBeginAndMaxQuerySize`
fires UBSan at the exact CIDB signature (`Lexer.cpp:119:43`); with the
fix, the test passes.
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov @rschu1ze — could you review this? It addresses three sanitizer findings accumulating on the Unit tests (msan|asan_ubsan, function_prop_fuzzer) master job: UBSan null-pointer arithmetic in Lexer::nextToken, MSan use-of-uninitialized-value in getURLScheme on empty URL, and MSan use-of-uninitialized-value in Poco::UTF8Encoding::queryConvert — the last one is fixed at the root in UTF8::convertUTF8ToCodePoint so sibling callers in Volnitsky.h, StringSearcher.h, translate.cpp, initcapUTF8.cpp, and FunctionsStringDistance.cpp are covered too. The Lexer bug is deterministically reproduced locally under ASan+UBSan; see the gtest LexerTest.NextTokenWithNullBeginAndMaxQuerySize in the diff.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

a) Deterministic repro? Yes — Lexer::nextToken UBSan finding reproduces on demand under the existing ASan+UBSan build:

build-asan/src/unit_tests_dbms --gtest_filter='LexerTest.NextTokenWithNullBeginAndMaxQuerySize'
/home/ubuntu/nerve-workspace/ClickHouse/src/Parsers/Lexer.cpp:119:43: runtime error: applying non-zero offset 262144 to null pointer

This matches the CIDB signature byte-for-byte. The MSan findings (getURLScheme empty input, convertUTF8ToCodePoint zero length) are not reproducible without an MSan build locally, but the bug mechanism is visible in the source and the CI function_prop_fuzzer MSan job is the authoritative verifier.

b) Root cause explained?

  • Lexer.cpp:119 — if (max_query_size && res.end > begin + max_query_size) computes begin + max_query_size before checking that begin is non-null. With begin == nullptr and max_query_size = 262144, nullptr + 262144 is UB. The fuzzer constructs Lexer(nullptr, nullptr, 262144).
  • protocol.h:17 — getURLScheme reads *pos where pos = data, without first checking size. With size == 0, the read is one byte past the buffer. Production ColumnString has null-terminator padding that hides this, but the fuzzer drives the function with unpadded buffers.
  • UTF8Helpers.cpp — Poco::UTF8Encoding::queryConvert reads the first byte without a length check. UTF8::convertUTF8ToCodePoint forwards directly, so any caller passing end - pos == 0 (hasSubsequenceUTF8, Volnitsky.h putNGramBase, StringSearcher.h, …) triggers MSan.

c) Fix matches root cause? Yes, each fix targets the identified mechanism:

  • Lexer: null-check begin before arithmetic.
  • getURLScheme: short-circuit size == 0.
  • convertUTF8ToCodePoint: short-circuit in_length == 0. I deliberately placed this fix at convertUTF8ToCodePoint (not per-caller) because the same bug class was reported at Volnitsky.h and conceivably at any other caller that passes end - pos. The root-cause fix covers all six current callers without whack-a-mole per-site guards.

d) Test intent preserved? / New tests added? Yes — no existing tests were weakened. New tests:

  • gtest_Lexer.cpp: 3 cases — null-begin + max_query_size > 0, empty-but-non-null buffer + max_query_size > 0, and a real-query sanity case.
  • 04113_sanitizer_empty_string_ops.sql: exercises protocol('') and hasSubsequenceCaseInsensitiveUTF8 with all four empty/non-empty combinations, plus non-ASCII UTF-8.

e) Both directions demonstrated? For the Lexer bug, yes — verified locally. Confirmed sequence:

  1. With fix applied → LexerTest.* 3/3 pass under ASan+UBSan.
  2. Lexer fix temporarily reverted, unit_tests_dbms rebuilt → LexerTest.NextTokenWithNullBeginAndMaxQuerySize fires UBSan at Lexer.cpp:119:43 with the exact CIDB signature.
  3. Fix re-applied, rebuilt → back to 3/3 pass.

The MSan fixes are backed by code analysis + regression tests and will be verified by the Unit tests (msan, function_prop_fuzzer) CI job on this PR.

f) Fix is general, not a narrow patch? Yes — the convertUTF8ToCodePoint fix is intentionally at the root-cause site rather than at each caller. I audited every current caller of convertUTF8ToCodePoint (HasSubsequenceImpl.h, Volnitsky.h, StringSearcher.h, translate.cpp, initcapUTF8.cpp, FunctionsStringDistance.cpp) — every one of them passes end - pos style arguments that can be zero at string boundaries, so the single fix covers all of them and any future callers. The Lexer and getURLScheme fixes are narrowly scoped because their bugs are at the only dereference site.

Session: cron:clickhouse-ci-task-worker:20260424-074500

@alexey-milovidov

Copy link
Copy Markdown
Member

See also #102302

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Apr 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [85a52f2]


AI Review

Summary

This PR fixes three sanitizer-reported edge cases (Lexer::nextToken, getURLScheme, and UTF8::convertUTF8ToCodePoint) by adding zero-length/null guards and adds regression coverage in both unit and stateless SQL tests. The changes are localized, address the reported failure modes directly, and do not introduce compatibility or rollout risks.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Apr 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.20% 76.20% +0.00%

Changed lines: 100.00% (26/26) | lost baseline coverage: 4 line(s) · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks for the pointer to #102302 @alexey-milovidov.

Closing this PR as superseded — PR #103489 was merged ~2h ago by @yakov-olkhovskiy landing identical fixes for all three areas this PR covered: Lexer::nextToken null-pointer arithmetic, getURLScheme empty-input guard, and HasSubsequenceImpl::hasSubsequenceUTF8 empty-haystack early return.

Master status (CIDB, 2026-04-24 12:45 UTC):

  • Unit tests (asan_ubsan, function_prop_fuzzer) — 4/4 OK in the most recent hour, 0 failures in the last 48h
  • Unit tests (msan, function_prop_fuzzer) — 4/4 OK, 0 failures
  • Unit tests (tsan, function_prop_fuzzer) — 4/4 OK, 0 failures
  • No UTF8Helpers / Volnitsky::putNGramBase / Poco::UTF8Encoding::queryConvert sanitizer hits anywhere (master or PRs) in 24h

The unique delta in this PR was moving the UTF-8 guard from HasSubsequenceImpl.h one layer up into UTF8::convertUTF8ToCodePoint (covering all 6 callers: HasSubsequenceImpl, Volnitsky, StringSearcher, translate, initcapUTF8, FunctionsStringDistance). Since there are no master-visible sanitizer signatures from the other callers right now, that generalization is pre-emptive. If Volnitsky::putNGramBase or UTF8Encoding::queryConvert reappear on master, we'll file a narrow follow-up PR with just that root-cause hardening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants