Skip to content

Fix UB in 32-bit shift before widening to UInt64 - #105633

Merged
Algunenano merged 2 commits into
ClickHouse:masterfrom
Algunenano:fix-shift-ub-issue-105629
May 23, 2026
Merged

Algunenano merged 2 commits into
ClickHouse:masterfrom
Algunenano:fix-shift-ub-issue-105629

Conversation

@Algunenano

Copy link
Copy Markdown
Member

Closes #105629.

Both call sites computed 1 << n in signed 32-bit arithmetic before widening the result to UInt64. The shift count can reach values where this is undefined behavior:

  • src/Common/hasLinuxCapability.cpp:64 — Linux capabilities >= 31 exist (CAP_MAC_OVERRIDE, CAP_SYSLOG, CAP_AUDIT_READ, CAP_PERFMON, CAP_BPF, CAP_CHECKPOINT_RESTORE, ...). Current in-tree callers pass values < 31.
  • src/Common/MortonUtils.h:198 — only reached from unit tests; the function is not used by the server.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

@clickhouse-gh

clickhouse-gh Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e162fef]

Summary: ✅


AI Review

Summary

This PR removes undefined behavior from two bit-shift sites by widening the literal before shifting (UInt64(1) << ...) in hasLinuxCapability and hyperrectangleToPossibleMortonIntervals. I reviewed the current diff and surrounding code paths, checked prior PR discussion/threads, and did not find unresolved correctness, safety, or performance concerns in the updated code.

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 May 22, 2026
@Algunenano

Copy link
Copy Markdown
Member Author

Tried to debug the coredump (which is unrelated) but couldn't because it's truncated -> #105631

mysql test fixed here: #105636

@clickhouse-gh

clickhouse-gh Bot commented May 22, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 84.10% +0.30%
Functions 91.40% 91.40% +0.00%
Branches 76.00% 76.50% +0.50%

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

Full report · Diff report

@Algunenano
Algunenano added this pull request to the merge queue May 23, 2026
Merged via the queue into ClickHouse:master with commit 3646295 May 23, 2026
327 of 328 checks passed
@Algunenano
Algunenano deleted the fix-shift-ub-issue-105629 branch May 23, 2026 13:17
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 23, 2026
DavidHe-2008 pushed a commit to DavidHe-2008/ClickHouse that referenced this pull request Jun 1, 2026
…ue-105629

Fix UB in 32-bit shift before widening to UInt64
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 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.

[SAST] Potential undefined behavior in bitwise shift: (1 << result_bit_idx) and (1 << cap) use signed int before widening to UInt64

3 participants