Skip to content

Fix reading past the buffer after an escape sequence in TSV with input_format_tsv_crlf_end_of_line - #123861

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-tsv-crlf-escape-at-buffer-end
Oct 5, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-tsv-crlf-escape-at-buffer-end

Conversation

@groeneai

@groeneai groeneai commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #119168

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a segmentation fault or std::length_error when reading TSV formats with input_format_tsv_crlf_end_of_line = 1 if an escape sequence ended exactly at the end of a read buffer.

Description

On 26.9 and 26.8, SELECT * FROM file('data.tsv', 'TSVWithNames', 's String') with input_format_tsv_crlf_end_of_line = 1 segfaults when a field's escape sequence ends exactly at the end of a read buffer and the next byte in memory is \r; on 26.3 and 25.3 the same query fails with std::length_error. The same over-read is the MemorySanitizer: use-of-uninitialized-value in readEscapedStringIntoImpl found by BuzzHouse (amd_msan): https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119168&sha=39d344b70a3cd3857d754e8b16832e4ddbdb802f&name_0=PR&name_1=BuzzHouse%20%28amd_msan%29

readEscapedStringIntoImpl checked for \r right after an escape sequence without checking that the buffer still had data, so it read the byte after the working buffer (inside the padding, so ASan is silent, and uninitialized for MSan). A stale \r there moved the position past the end of the buffer, and the next append got a negative length. ReadBufferFromIStream and the HTTP buffer from the report are not at fault: file() hits the same path.

The \r check is now the else of the escape branch, so it only sees a \r found by find_first_symbols. After an escape sequence the loop refills the buffer and a following \r takes the same branch, so the result is unchanged for every input away from the boundary.

The test reads an 18-byte file with max_read_buffer_size = 16, so the second read holds only \t and the next byte is a stale \r. Without the fix each of its three queries (header, data row, schema inference) aborts a debug build with Logical error: 'pos_addr <= end_addr'.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/123861]

Version info

  • Merged into: 26.10.1.1569-master (included in 26.10 and later)
  • Backported to: 26.9.11.2, 26.8.18.3, 26.7.22.7, 26.3.42.4

… line endings

With `input_format_tsv_crlf_end_of_line = 1`, `readEscapedStringIntoImpl`
checked for '\r' right after handling an escape sequence, without checking
that the buffer still had data. When the escape sequence ended exactly at the
end of the working buffer, it read the byte past the end. That byte is inside
the buffer padding, so ASan does not see it, but MSan reports it as
uninitialized (BuzzHouse amd_msan, STID 2798-4afa, via `url()` and
`ReadBufferFromIStream`). If the stale byte was '\r', the position moved past
the end of the buffer: debug builds abort on `pos_addr <= end_addr`, release
builds throw `std::length_error` or segfault.

The CR branch is only meant for the '\r' found by `find_first_symbols`, so it
is now the `else` of the escape branch. After an escape sequence the loop goes
back to `while (!buf.eof())`, which refills the buffer, and a following '\r'
is found by the next `find_first_symbols` and handled by the same branch, so
the result is unchanged for every input that does not hit the boundary.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 4, 2026
@groeneai

groeneai commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings) plus my own review.

# Sev Finding Verdict Evidence / action
1 ⚠️ The test output is the same with input_format_tsv_crlf_end_of_line = 0, so it may not exercise the changed branch DISAGREE Without the fix the test fails (server aborts on pos_addr <= end_addr with the setting on), and the same query with the setting off passes on the unfixed build, so the failure comes only from the changed branch. Output for escape sequences followed by an in-buffer \r (x\t\r\n, w\N\r\n, ...) is byte-identical before and after the fix.
2 💡 Use Critical Bug Fix and a present-tense entry DISAGREE Bug Fix is measured on 26.9/26.8 (segfault) and 26.3/25.3 (std::length_error); Critical Bug Fix is left to maintainers.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261004-132200

@clickhouse-gh

clickhouse-gh Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [eb93d46]

Summary: ✅


AI Review

Summary

This PR fixes the TSV / TSVWithNames CRLF parser so that, after consuming an escape sequence, it no longer re-reads *buf.position() as if it were still on the delimiter-dispatch path. That closes the out-of-bounds read at a buffer boundary without changing normal \r handling, and the new stateless test targets the three affected paths here: header skipping via NullOutput, typed string deserialization via PaddedPODArray<UInt8>, and raw TSV field/schema-inference parsing via String.

Final Verdict

✅ No findings. The fix matches the stated bug, the test covers the affected paths, prior discussion does not leave an unresolved issue, and CI is all green.

LLVM Coverage Report

Measured on commit eb93d46.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 92.10% 92.20% +0.10%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 1/1 (100.00%) · Uncovered code

Full report · Diff report

@groeneai
groeneai requested review from Avogar and vitlibar October 4, 2026 13:44
@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 4, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Oct 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing eb93d46b8 with master 2493c465d (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 8 s compile time in total, 7 of them have a recent master baseline.

Job report

@PedroTadim PedroTadim self-assigned this Oct 5, 2026
@PedroTadim PedroTadim added v26.8-must-backport pr-must-backport Pull request should be backported intentionally. Use this label with great care! and removed v26.8-must-backport labels Oct 5, 2026
@PedroTadim

Copy link
Copy Markdown
Member

The sync PR failure is unrelated

@PedroTadim
PedroTadim added this pull request to the merge queue Oct 5, 2026
Merged via the queue into ClickHouse:master with commit 961b4f7 Oct 5, 2026
149 of 150 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 5, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 5, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 5, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 5, 2026
Backport #123861 to 26.9: Fix reading past the buffer after an escape sequence in TSV with input_format_tsv_crlf_end_of_line
clickhouse-gh Bot added a commit that referenced this pull request Oct 5, 2026
Backport #123861 to 26.8: Fix reading past the buffer after an escape sequence in TSV with input_format_tsv_crlf_end_of_line
PedroTadim added a commit that referenced this pull request Oct 5, 2026
Backport #123861 to 26.3: Fix reading past the buffer after an escape sequence in TSV with input_format_tsv_crlf_end_of_line
clickhouse-gh Bot added a commit that referenced this pull request Oct 5, 2026
Backport #123861 to 26.7: Fix reading past the buffer after an escape sequence in TSV with input_format_tsv_crlf_end_of_line
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 6, 2026
… an escape sequence in TSV with input_format_tsv_crlf_end_of_line
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 9, 2026
… an escape sequence in TSV with input_format_tsv_crlf_end_of_line
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 9, 2026
… an escape sequence in TSV with input_format_tsv_crlf_end_of_line
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 9, 2026
… an escape sequence in TSV with input_format_tsv_crlf_end_of_line
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 comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants