Skip to content

Fix h3Line returning uninitialized memory when the line crosses pentagon distortion - #116583

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-h3line-uninitialized-memory-pentagon
Aug 27, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-h3line-uninitialized-memory-pentagon

Conversation

@groeneai

@groeneai groeneai commented Aug 26, 2026 •

Copy link
Copy Markdown
Collaborator

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 h3Line returning uninitialized memory in the tail of the result array when a line between two valid H3 indices cannot be drawn because it crosses pentagon distortion. Such a call now throws INCORRECT_DATA.

Description

h3Line builds its result in two passes: a sizing pass that calls gridPathCellsSize and checks its H3Error, then PODArray::resize, which does not initialize elements, then a fill pass that called gridPathCells and discarded its H3Error.

The two passes validate different things. gridPathCellsSize validates only the two endpoints, while gridPathCells walks the intermediate cells and returns E_PENTAGON after a partial write when the line crosses pentagon distortion. The slots it never reached were returned to the client:

SELECT h3Line(toUInt64(585609238802333695), toUInt64(585624631965122559));

On master this returns a 3-element array whose third element is not a valid H3 index. The observed values were pointer-shaped and changed between calls, so a constant query with plain SELECT discloses server memory.

This fix checks the error, matching the sizing pass above and the sibling h3HexRing, which already checks it. h3Line and h3HexRing are the only two h3 functions that fill a resize()d PODArray this way, so the whole class now checks; the other h3 functions that discard an H3Error fill value-initializing containers.

gridPathCells fails only by returning early from its fill loop, so it never fails after writing every slot. The calls that now throw are exactly those that previously returned a partly uninitialized array. That includes functions_h3_default_if_invalid = 1: it substitutes a default for an invalid cell, and both endpoints here are valid. A test arm pins it.

Validation: the three new arms fail on master, pass with the fix, and survive 50 randomized-settings runs; the two untouched h3Line tests pass on both; the same pair throws through materialize, Nullable, LowCardinality and batched shapes, and non-pentagon inputs stay byte-identical to master.

Reported to me privately as a core-security issue, with @ nikitamikhaylov asking for a public fix. The carrier is public code, so the fix belongs here; there is no public issue to link.


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

Version info

  • Merged into: 26.9.1.194 (included in 26.9 and later)
  • Backported to: 26.8.3.57, 26.7.7.38, 26.6.5.65, 26.3.33.10

h3Line builds its result in two passes. The sizing pass calls
gridPathCellsSize and checks its H3Error, the result is then sized with
PODArray::resize, which does not initialize elements, and the fill pass
called gridPathCells and discarded the returned H3Error.

The two passes do not validate the same thing. gridPathCellsSize
validates only the two endpoints, while gridPathCells walks the
intermediate cells and returns E_PENTAGON after a partial write when the
line crosses pentagon distortion. The slots it never reached were
returned to the client, so

  SELECT h3Line(toUInt64(585609238802333695), toUInt64(585624631965122559))

handed back a 3-element array whose third element was raw heap. The
observed values were live heap addresses that varied between calls, and a
plain SELECT with constant arguments is enough to reach it.

Check the error, matching the sizing pass in the same function and the
sibling h3HexRing, which has the identical two-pass shape and already
checks it. h3Line and h3HexRing are the only two h3 functions that fill a
resize()d PODArray this way, so the whole class now checks.

The new arm in 04034_h3_functions_default_if_invalid pins that this also
throws under functions_h3_default_if_invalid = 1: that setting
substitutes a default for an invalid cell, and both endpoints here are
valid.

Co-Authored-By: Claude Opus 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 Aug 26, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal review and pre-PR validation gate (click to expand)

Reviewed cold by me, then twice by an independent second model over the full diff: findings=0
both times. No blockers, no majors. Two nits I am disclosing rather than fixing:

  • 💡 The new comment in 02293_h3_line.sql ends "rather than return a partially written array", which
    leans on the two-pass buffer shape; the preceding clause already carries the intent on its own.
  • 💡 The commit message does not wrap identifiers in inline code blocks, which .claude/CLAUDE.md
    asks for. The PR title and description do.

One thing noticed and deliberately not pursued: the other h3 functions that discard an H3Error
cannot disclose memory, but their worst case is a silent 0 where an error belongs. That is a
diagnostics concern in code this PR does not touch, so it is not folded in here.

# Question Answer
a Deterministic repro? Yes. One constant query, plain SELECT, no settings: SELECT h3Line(toUInt64(585609238802333695), toUInt64(585624631965122559)) returns a 3-element array whose 3rd element fails h3IsValid. 100% reproducible, not intermittent.
b Root cause explained? Yes. The sizing pass checks gridPathCellsSize, then PODArray::resize allocates without initializing, then the fill pass discarded the H3Error from gridPathCells. gridPathCellsSize validates only the two endpoints; gridPathCells walks the intermediate cells and returns E_PENTAGON after a partial write. The unwritten tail was returned to the client.
c Fix matches root cause? Yes. The discarded error is checked at the site that discarded it. No widened bound, no no-random-* tag, no data reduction, and no guard at a symptom site.
d Test intent preserved / new tests added? Yes. Three arms added to two existing test files; no new file, no test removed, weakened or retagged; both .reference files byte-unchanged (every new arm is a serverError assertion). The pre-existing 28-row ordinary-line query in 02293_h3_line is the keep-working control and still passes.
e Both directions demonstrated? Yes, per arm and per file, on Build-ID-verified servers. All three arms report The query succeeded but the server error '117' was expected on master and pass with the fix. 02293_h3_line and 04034_h3_functions_default_if_invalid FAIL on master, pass with the fix; the two untouched h3Line tests pass on both. 50 randomized runs of both touched files: 100/100 passed.
f Fix is general across code paths? Yes. h3Line and h3HexRing are the only two h3 functions that fill a resize()d PODArray through ptr + current_offset. h3HexRing already checked its fill-pass error; after this change the unchecked list for that class is empty. h3PolygonToCells* zero-fill and check both passes. The remaining h3 functions that discard an H3Error write into value-initializing containers, so they disclose nothing and are out of scope here.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. The pentagon pair throws INCORRECT_DATA as a plain constant, under materialize, as Nullable, LowCardinality, LowCardinality(Nullable), in a 2-row batch with the failing row either first or last, and under functions_h3_default_if_invalid = 1. useDefaultImplementationForConstants() is true, so all of these reach one executeImpl. Nine non-pentagon shapes (ordinary line, Nullable, LowCardinality, NULL argument, invalid cell with the setting on and off, start == end, the far-apart pair, a multi-row batch) are byte-identical to master.
h Backward compatible? (maintainer-approved exception only) No setting default changed, so no SettingsChangesHistory.cpp entry is required, and no serialization or on-disk format is touched. There is a deliberate user-visible behavior change: a query that previously "succeeded" now errors. No correct program can depend on the old value, because it was uninitialized memory that varied between calls. Changelog category Bug Fix (user-visible misbehavior in an official stable release).
i Invariants and contracts preserved? Yes. Invariant: every slot of the output PODArray that dst_offsets claims is populated has actually been written before the column is returned. A partial write on success is impossible, since gridPathCells writes exactly distance + 1 slots when it returns E_SUCCESS, so checking the error is sufficient and no written-count bookkeeping is needed. On the throw path the two local mutable columns are destroyed by unwinding, so nothing partial is observable and nothing leaks. The function takes no locks and touches no shared state.

Session ids: impl cron:clickhouse-impl-slot-41:20260826-181800, review cron:clickhouse-review-slot-10:20260826-185600

@clickhouse-gh clickhouse-gh Bot closed this Aug 26, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Aug 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9b7bee6]

Summary: ❌

job_name test_name status info comment
Docs examples FAIL
Documentation examples FAIL cidb IGNORED

AI Review

Summary

This PR fixes h3Line so the fill pass now propagates gridPathCells failures instead of returning a partially unwritten PODArray tail to the caller. I did not find a remaining correctness, safety, or contract issue in the current diff, and the added stateless tests cover the new exception path for both the default behavior and functions_h3_default_if_invalid = 1.

Missing context / blind spots
  • ⚠️ Full CI was not complete at review time: Build (arm_tidy), the aggregate PR report, and CH Inc sync were still pending. Green completion of those jobs would close the remaining platform/build-validation gap.
Final Verdict

✅ No blocking issues found in the current diff.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.30% 87.30% +0.00%
Functions 91.90% 92.00% +0.10%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 9/9 (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 26, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @nikitamikhaylov, here is the public fix you asked for. h3Line sized its output with gridPathCellsSize, which validates only the two endpoints, then discarded the H3Error from gridPathCells, which fails after a partial write when the line crosses pentagon distortion, so the unwritten tail of the PODArray went back to the client.

@clickhouse-gh

clickhouse-gh Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9b7bee649 with master f10973ea7 (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 711.80 MiB 708.74 MiB -3.06 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, 6 s compile time in total, 7 of them have a recent master baseline.

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 9b7bee6

Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Docs examples / Documentation examples stale tests/docs_examples/known_failures.txt:50 entry Function/catboostEvaluate#0, invalid since #109710 removed the integration #116566 (external, merged) - the revert restores catboostEvaluate; merged 20:09:12Z but this branch is 127 commits behind it, so the tree under test lacks it
Stateless tests (arm_asan_ubsan, targeted) / AddressSanitizer: heap-use-after-free (STID 1003-358c) + Server liveness check failed pre-existing UAF at ExecutingGraph::updateNode (ExecutingGraph.cpp:422), freed by removeReadyGroups; unreachable from this 3-file h3Line diff. Re-ran green on this same head #116103 (ours, open)
Stress test (arm_asan_ubsan, s3) / A data stream row of AlignStreams has no matching metadata stream row (STID 0250-16db) trunk regression introduced by merged #116128; also fires on master. Re-ran green on this same head #116531 (external, open)

Praktika's own report for this head is OK 155 / SKIPPED 19 / FAIL 1, the single FAIL being
Docs examples; the other two rows above are earlier attempts on this head that passed on re-run,
listed here so their owners are on record. Mergeable Check and PR are aggregate roll-ups of that
one failure, not separate failures. 175 of 175 check-runs are complete, Finish Workflow is green,
and no job was dropped. Nothing here is caused by this PR: Style check, both Fast test lanes,
Build (arm_tidy) and every Bugfix validation lane are green.

Session id: cron:our-pr-ci-monitor:20260827-013000

@PedroTadim PedroTadim self-assigned this Aug 27, 2026
@PedroTadim
PedroTadim added this pull request to the merge queue Aug 27, 2026
Merged via the queue into ClickHouse:master with commit 9c421bd Aug 27, 2026
354 of 360 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 27, 2026
@nikitamikhaylov nikitamikhaylov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Sep 7, 2026
PedroTadim added a commit that referenced this pull request Sep 7, 2026
Cherry pick #116583 to 26.7: Fix `h3Line` returning uninitialized memory when the line crosses pentagon distortion
PedroTadim added a commit that referenced this pull request Sep 7, 2026
Cherry pick #116583 to 26.6: Fix `h3Line` returning uninitialized memory when the line crosses pentagon distortion
PedroTadim added a commit that referenced this pull request Sep 7, 2026
Cherry pick #116583 to 26.3: Fix `h3Line` returning uninitialized memory when the line crosses pentagon distortion
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 7, 2026
robot-clickhouse added a commit that referenced this pull request Sep 7, 2026
… when the line crosses pentagon distortion
robot-clickhouse added a commit that referenced this pull request Sep 7, 2026
… when the line crosses pentagon distortion
robot-clickhouse added a commit that referenced this pull request Sep 7, 2026
… when the line crosses pentagon distortion
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 7, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 7, 2026
… when the line crosses pentagon distortion
clickhouse-gh Bot added a commit that referenced this pull request Sep 7, 2026
Backport #116583 to 26.8: Fix `h3Line` returning uninitialized memory when the line crosses pentagon distortion
clickhouse-gh Bot added a commit that referenced this pull request Sep 7, 2026
Backport #116583 to 26.6: Fix `h3Line` returning uninitialized memory when the line crosses pentagon distortion
clickhouse-gh Bot added a commit that referenced this pull request Sep 7, 2026
Backport #116583 to 26.7: Fix `h3Line` returning uninitialized memory when the line crosses pentagon distortion
PedroTadim added a commit that referenced this pull request Sep 8, 2026
Backport #116583 to 26.3: Fix `h3Line` returning uninitialized memory when the line crosses pentagon distortion
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 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.

6 participants