Skip to content

Fix UBSan error in Lexer::nextToken: null pointer arithmetic - #102302

Closed
alexey-milovidov wants to merge 11 commits into
masterfrom
fix-lexer-null-begin-ubsan
Closed

alexey-milovidov wants to merge 11 commits into
masterfrom
fix-lexer-null-begin-ubsan

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Apr 9, 2026 •

Copy link
Copy Markdown
Member

When Lexer is constructed with a null buffer (begin = nullptr) and non-zero max_query_size, the expression begin + max_query_size in Lexer::nextToken is undefined behavior (adding a non-zero offset to a null pointer). This was triggered by the FunctionsStress.stress test in CI.

The fix adds a null check for begin before the pointer arithmetic.

Changelog category (leave one):

  • Not For Changelog

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

Fix undefined behavior in Lexer when constructed with a null buffer and non-zero max_query_size.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

When `Lexer` is constructed with a null buffer (begin = nullptr) and
non-zero `max_query_size`, the expression `begin + max_query_size` is
undefined behavior. This was triggered by the `FunctionsStress.stress`
test in CI.

Add a null check for `begin` before the pointer arithmetic.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Apr 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f335e91]

Summary: ❌

job_name test_name status info comment
Stress test (arm_tsan) FAIL
Hung check failed, possible deadlock found FAIL
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/check_report_messages.py FAIL
Stateless tests (arm_asan_ubsan, targeted) ERROR

AI Review

Summary

This PR fixes undefined behavior in Lexer::nextToken by guarding pointer arithmetic with a null check on begin, and adds a focused unit test for the Lexer(nullptr, nullptr, max_query_size) path. I found no remaining correctness, safety, or rollout issues in the final patch set.

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-not-for-changelog This PR should not be mentioned in the changelog label Apr 9, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

alexey-milovidov and others added 2 commits April 10, 2026 04:01
…= 1`

The test case `SELECT formatQueryOrNull('SELECT 1') SETTINGS max_query_size = 1`
fails because `max_query_size = 1` applies to the outer query's parsing on the
server, not just to `formatQueryOrNull`'s internal parsing. The outer query
exceeds 1 byte and gets a syntax error before the function is even called.

The empty string test case is sufficient to exercise the null-buffer Lexer path,
and the unit test in `gtest_Lexer.cpp` covers the direct path.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@@ -0,0 +1,6 @@
-- Regression test: Lexer with null begin pointer and non-zero max_query_size

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case does not actually cover the nullptr-begin path from the UB report. In formatQueryOrNull, begin is taken from ColumnString storage (&data[prev_offset]), so for '' it is an empty buffer pointer (begin == end) but still non-null. That means this SQL test would pass even without the null-pointer arithmetic fix in Lexer.

Please either adjust the test description to avoid claiming null begin pointer coverage, or add a path that truly constructs Lexer(nullptr, nullptr, max_query_size) (the new gtest_Lexer already does this).

alexey-milovidov and others added 2 commits April 26, 2026 16:34
The SQL test exercises `formatQueryOrNull('')`, where `begin` is a
non-null empty-buffer pointer from `ColumnString` storage, not
`nullptr`. The actual null-pointer arithmetic path that triggered
UBSan in `Lexer::nextToken` is covered directly by
`gtest_Lexer.cpp` (`Lexer.NullBuffer`).

Update the comment so it does not claim coverage of the null
begin pointer case.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 100.00% (2/2) · Uncovered code

Full report · Diff report

@CheSema

CheSema commented Apr 27, 2026

Copy link
Copy Markdown
Member

Some how the code with the fix is vanished from this PR.
The issue has been secured in #103489
The similar tests has been added here already.

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

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants