Repository navigation
SipHash::update: short-circuit on empty input - #106469
Conversation
`SipHash::update(const char * data, UInt64 size)` evaluates `data + size` and `data + 8` near the start of the unrolled loop. When `data == nullptr` and `size == 0`, the loop body never executes (the condition `nullptr + 8 <= nullptr` is false), but evaluating `data + 8` on a null pointer with non-zero offset is UB per `[expr.add]/4`. UBSan flags this under `-fsanitize=pointer-overflow`. The trigger surfaced via `SerializationInterval::getHash` -> `SipHash::update(std::string_view sv)` -> `update(sv.data(), sv.size())` when an out-of-range `IntervalKind` produced an empty `std::string_view` with `data() == nullptr` from `magic_enum::enum_name` on libcxx (STID 3285-3bba in the AST fuzzer). The primary fix for that specific path lives in PR ClickHouse#106261 (range check at the deserialization site). This change is a narrow precondition for `SipHash::update`: an empty input is a no-op. `ALWAYS_INLINE` keeps the hot path unchanged for non-empty inputs and the single zero-size branch folds away for callers that pass a constant non-zero size. Any future caller that legitimately hands `SipHash::update` a `(nullptr, 0)` pair (e.g. an empty string_view from another magic_enum-style source) will be a no-op instead of re-tripping UBSan. Adds a small gtest (`gtest_sip_hash.cpp`) that: 1. Verifies updating with an empty std::string_view followed by other data and a final empty update does not invoke UB and produces a stable digest. 2. Verifies the empty updates are observably no-ops: the digest equals what you would get hashing the same payload without the empty prefix/suffix updates. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-ci-task-worker:20260604-133700 |
@azat asked for the SipHash defense-in-depth tweak (size==0 short-circuit plus the gtest) to be split out of this PR so the user-visible bug fix (out-of-range IntervalKind binary decoding -> INCORRECT_DATA) can be backported independently. The SipHash hunk is now PR ClickHouse#106469. This PR keeps only the IntervalKind range check at the deserialization site plus the regression test. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Workflow [PR], commit [9cad85a] Summary: ✅
AI ReviewSummaryThis PR makes empty input a no-op for Findings❌ Blockers
Final VerdictStatus: ❌ Block Minimum required action: fix the |
|
CI is fully finished on CI ledger for
PR is APPROVED. Both failures are pre-existing chronic trunk issues unrelated to the SipHash size==0 short-circuit. |
|
@groeneai merge with master please |
…-siphash-empty-guard
|
@azat merged master into the branch. Merge commit: Session: cron:clickhouse-ci-task-worker:20260608-164800 |
| ALWAYS_INLINE void update(const char * data, UInt64 size) | ||
| { | ||
| /// Avoid UB from `data + 8` arithmetic below when `data == nullptr`. | ||
| if (size == 0) |
There was a problem hiding this comment.
size == 0 only handles the nullptr, 0 trigger, but the UB root cause remains for ordinary non-empty inputs: the loop below evaluates data + 8 before checking whether 8 bytes remain. For a 1-7 byte buffer, and again after the last full 8-byte chunk when data == end, that expression is outside the valid [data, end] range, so SipHash::update still has undefined behavior on valid short inputs. Could we make the loop use pointer subtraction instead, for example while (end - data >= 8), so no out-of-range pointer is formed?
There was a problem hiding this comment.
Good catch, technically correct: data + 8 is also formed when data == end after the last full chunk, and for 1-7 byte buffers when end - data < 8 on the very first iteration; both are out-of-range pointer expressions and UBSan's pointer-overflow will flag them under the right build flags.
I'd like to keep this PR narrow per @azat's split request (a 2-line size == 0 short-circuit addressing the (nullptr, 0) STID 3285-3bba trigger), so I won't widen the scope here. I'll file a separate follow-up PR rewriting the unrolled loop to use end - data >= 8 (and audit the rest of update for the same shape). Tracking it now.
There was a problem hiding this comment.
Filed the follow-up: #107103. It rewrites the loop condition to while (end - data >= 8) so data + 8 is never formed past end for short inputs, with byte-identical hash output and boundary-length gtest cases. Thanks for catching the general case.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 18/18 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
CI fully finished on CI Ledger — covered SHA: 9cad85a
PR APPROVED by @ azat on prior SHA |
…d loop The unrolled 8-byte loop used `while (data + 8 <= end)`. When fewer than 8 bytes remain (a 1-7 byte input, or the tail after the last full chunk where data == end), `data + 8` points past the one-past-the-end pointer `end`. Forming such a pointer is undefined behavior under [expr.add]/4, independent of whether it is dereferenced. This is the general form of the same defect class as the empty-input case fixed in ClickHouse#106469: there `data` was nullptr and `data + 8` triggered UBSan's "applying non-zero offset to null pointer"; here `data` is a valid non-null pointer but the formed pointer still leaves the [base, base+size] range. Rewrite the condition as `end - data >= 8`, which only ever evaluates pointer subtraction and never forms a pointer outside the buffer. The loop body is unchanged, so the computed hash is identical for every input length. The `size == 0` early return added in ClickHouse#106469 is now redundant and is removed. After the rewrite the empty input path forms no out-of-range pointer: `end = data + size` is `nullptr + 0` (well-defined), and the only remaining pointer arithmetic is the subtractions `end - data` (all `nullptr - nullptr`, i.e. 0), so the loop body and the tail switch never run. This is the same as a zero-length valid buffer. Verified under clang-21 `-fsanitize=address,undefined -fno-sanitize-recover=all`: the existing empty-string-view tests from ClickHouse#106469 and the new boundary/chunk tests all pass and produce byte-identical hashes with the guard removed. Found by clickhouse-gh[bot] inline review on ClickHouse#106469. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Related: #106261
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description / motivation
Split out from PR #106261 per @azat: keep the user-visible bug fix (out-of-range IntervalKind binary decoding) on PR #106261 so it can be backported, and isolate this defense-in-depth tweak to
SipHash::updatehere.SipHash::update(const char * data, UInt64 size)evaluatesdata + sizeanddata + 8early in the unrolled loop. Whendata == nullptrandsize == 0, the loop body never runs (nullptr + 8 <= nullptris false) but evaluatingdata + 8on a null pointer with non-zero offset is UB per[expr.add]/4. UBSan flags it under-fsanitize=pointer-overflow.The triggering path that surfaced this (STID 3285-3bba) was
SerializationInterval::getHash->SipHash::update(std::string_view)->update(sv.data(), sv.size())with an empty view whosedata() == nullptr(libcxx behaviour formagic_enum::enum_nameon an unknown enum value). That specific path is closed at the deserialization site by PR #106261. This PR is the narrow precondition: an empty input is a no-op.ALWAYS_INLINEkeeps the hot path unchanged for non-empty inputs and the size==0 branch folds away for callers that pass a constant non-zero size. Any future caller that handsSipHash::updatea(nullptr, 0)pair (e.g. an emptystd::string_viewfrom anothermagic_enum-style source) will be a no-op instead of re-tripping UBSan.Tests
src/Common/tests/gtest_sip_hash.cpp:UpdateWithEmptyStringViewDoesNotInvokeUB: hashing with an emptystd::string_viewfollowed by other data and a final empty update does not invoke UB and produces a stable digest. UBSan unit test target catches the original report on master.UpdateWithEmptyStringViewIsNoOp: empty updates are observably no-ops; the digest equals what you would get hashing the same payload without the empty prefix/suffix updates.cc @azat
Version info
26.6.1.523