Skip to content

Fix UBSan: negation of signed INT_MIN before cast to unsigned#96979

Merged
alexey-milovidov merged 2 commits intomasterfrom
fix-ubsan-negate-int-min
Feb 15, 2026
Merged

Fix UBSan: negation of signed INT_MIN before cast to unsigned#96979
alexey-milovidov merged 2 commits intomasterfrom
fix-ubsan-negate-int-min

Conversation

@alexey-milovidov
Copy link
Copy Markdown
Member

Summary

  • Fix undefined behavior in arrayElement where static_cast<UInt64>(-idx) negates INT_MIN in signed domain (UB). Changed to -static_cast<UInt64>(idx) which negates in unsigned domain.
  • Applied the same preventive fix in Planner, InterpreterSelectQuery, WindowTransform, and TemporaryDataOnDisk.
  • Added a regression test with toInt32(-2147483648) array index.

AST fuzzer (amd_ubsan) CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=36a049a7f14c6fbc7e0eb33dc32ec94514ceaa7c&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_ubsan%29

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

🤖 Generated with Claude Code

…igned

The expression `static_cast<UInt64>(-idx)` performs negation in signed
domain first, which is undefined behavior when `idx` equals `INT_MIN`
(e.g., -2147483648 for Int32). The fix reverses the operations:
`-static_cast<UInt64>(idx)` casts to unsigned first, then negates
in well-defined unsigned arithmetic.

Fixed in `arrayElement.cpp` (the actual UBSan failure from the AST
fuzzer) and preventively in `Planner.cpp`, `InterpreterSelectQuery.cpp`,
`WindowTransform.cpp`, and `TemporaryDataOnDisk.cpp`.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=36a049a7f14c6fbc7e0eb33dc32ec94514ceaa7c&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_ubsan%29

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh Bot commented Feb 15, 2026

Workflow [PR], commit [339320e]

Summary:

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Feb 15, 2026
@alexey-milovidov alexey-milovidov self-assigned this Feb 15, 2026
@alexey-milovidov alexey-milovidov merged commit ffb2121 into master Feb 15, 2026
138 of 139 checks passed
@alexey-milovidov alexey-milovidov deleted the fix-ubsan-negate-int-min branch February 15, 2026 22:09
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci 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.

2 participants