fix(hash): avoid undefined shift behavior for full-width bit masks #3367
All Vouch review confidence checks passed
Residual risk 0.15 = Change Risk (0.36) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.36 · 182 lines diff across 2 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.312
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #3367"
general
weight: 0.35
jan-wassenberg
Problem
In
hwy/contrib/hash/hash-inl.h, the compile-time maskkMaskis defined as:in
MaskedWeakTwoMulandMaskedTriple32, and similarly:in
MaskedMoremurandMaskedWeakXMX.When instantiated with full width (
kBits == 32orkBits == 64), the ternary operator's true branch is selected. However, the compiler still instantiates the false branch(uint32_t{1} << 32) - 1or(uint64_t{1} << 64) - 1. Shifting an integer by its bit width or greater is undefined behavior under the C++ standard and triggers compiler warnings such as MSVCwarning C4293: '<<': shift count negative or too big, undefined behavior.Solution
Mask the shift count with
(kBits & 31)for 32-bit types and(kBits & 63)for 64-bit types:kBits == 32/kBits == 64, the shift count becomes0, preventing UB while the ternary operator selects~0.kBits < 32/kBits < 64,kBits & 31/kBits & 63evaluates tokBits, preserving the exact intended mask.Verification
hash_test.exeon Windows (MSVC 2022).warning C4293is eliminated.TestAllAvalanche,TestAllBias,TestAllBuckets,TestAllBijection,TestAllEdgeCases,TestAllMasked).