Skip to content

Fix out-of-bounds read in formatRowNoNewline with empty rows - #115669

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-formatrow-nonewline-offset-underflow
Aug 21, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-formatrow-nonewline-offset-underflow

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 20, 2026 •

Copy link
Copy Markdown
Member

Fixes a bug in the function formatRowNoNewline. When a row is formatted to an empty result (for example, an empty string with the RawBLOB format), the newline-stripping logic could rewind the shared write buffer into the previous row's bytes, producing non-monotonic ColumnString offsets and a size_t underflow in the string size. Functions consuming the result could then return incorrect results or raise a logical error.

The trailing newline is now stripped only when the current row actually emitted at least one byte, which keeps the offsets monotonic. The cursor is also kept within the current working buffer so the decrement never moves it before the buffer start after a flush.

Added a regression test in tests/queries/0_stateless/04512_format_row_no_newline_empty_row_underflow.sql.

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 bug in the function formatRowNoNewline that could produce incorrect results or a logical error when a row is formatted to an empty result.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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

Version info

  • Backported to: 26.7.5.11, 26.6.4.21, 26.5.8.11, 26.3.21.9, 25.8.33.2

`formatRowNoNewline` strips a trailing newline from each row by
decrementing the shared write buffer position. The guard only checked the
position against the start of the whole buffer, not the start of the
current row. When a row is formatted to an empty result (for example, an
empty string with the `RawBLOB` format), the decrement rewound into the
previous row's bytes, producing non-monotonic `ColumnString` offsets and a
`size_t` underflow in the string size. Downstream functions then returned
incorrect results or raised a logical error.

Strip the trailing newline only when the current row actually emitted at
least one byte (`buffer.count` past the row start), which keeps the offsets
monotonic. Also keep the cursor within the current working buffer so the
decrement never moves it before the buffer start after a flush.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [43f7715]

Summary: ✅


AI Review

Summary

This PR fixes formatRowNoNewline so an empty formatted row no longer rewinds the shared write buffer into the previous row, which was breaking ColumnString offset monotonicity and could later produce incorrect results or a logical error. I did not find correctness, safety, test-coverage, or PR-metadata issues in the current diff; the fix matches the stated bug, the added stateless test covers both the empty-row regression and buffer-growth path, and the linked CI report is green.

Final Verdict

✅ No findings. The PR looks ready to merge.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.00% 87.00% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.30% +0.00%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 20, 2026
@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Aug 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 43f77155b with master ea3d0db80 (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
Binary Master PR Δ
programs/clickhouse-stripped 704.45 MiB 701.40 MiB -3.05 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

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

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Aug 21, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 21, 2026
Merged via the queue into master with commit 5bce845 Aug 21, 2026
182 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-formatrow-nonewline-offset-underflow branch August 21, 2026 04:54
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 21, 2026
@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 Aug 21, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 21, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Aug 21, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 21, 2026
Backport #115669 to 26.7: Fix out-of-bounds read in `formatRowNoNewline` with empty rows
alexey-milovidov added a commit that referenced this pull request Aug 21, 2026
Backport #115669 to 26.5: Fix out-of-bounds read in `formatRowNoNewline` with empty rows
alexey-milovidov added a commit that referenced this pull request Aug 21, 2026
Backport #115669 to 26.3: Fix out-of-bounds read in `formatRowNoNewline` with empty rows
alexey-milovidov added a commit that referenced this pull request Aug 21, 2026
Backport #115669 to 25.8: Fix out-of-bounds read in `formatRowNoNewline` with empty rows
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 25, 2026
Backport #115669 to 26.6: Fix out-of-bounds read in `formatRowNoNewline` with empty rows
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 28, 2026
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
OTHER: formatRowNoNewline producing an empty result -> wrong/exc.
Effect in the production build a394e92: its upstream test(s) 04512_format_row_no_newline_empty_row_underflow fail on it and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv).
Upstream patch unchanged, with its test(s): 04512_format_row_no_newline_empty_row_underflow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants