Fix undefined behavior in Huffman encoder for single-symbol input - #133
Open
alexey-milovidov wants to merge 1 commit into
Open
Fix undefined behavior in Huffman encoder for single-symbol input#133alexey-milovidov wants to merge 1 commit into
alexey-milovidov wants to merge 1 commit into
Conversation
build_code is invoked with len == 0 for the tree root. When the Huffman tree has a single symbol (e.g. constant input data, which yields a single distinct quantization code), the leaf is the root and the code length is zero, so out1 << (64 - len) shifts a 64-bit value by 64 bits. That is undefined behavior and aborts under UndefinedBehaviorSanitizer. Handle len == 0 explicitly (the code is empty, store 0), and add a symmetric guard for len >= 128 in the other branch. Found while integrating SZ3 into ClickHouse: ClickHouse/ClickHouse#108788
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
HuffmanEncoder::build_codeis invoked withlen == 0for the tree root (see thebuild_code(huffmanTree->qq[1], 0, 0, 0)call). When the Huffman tree has a single symbol — which happens for constant input data, where every value maps to the same quantization code — the leaf is the root, so the code length is zero and the encoder executes:Shifting a 64-bit value by 64 bits is undefined behavior in C++. It aborts under UndefinedBehaviorSanitizer (
shift exponent 64 is too large for 64-bit type) and is generally unsafe.Fix
Handle
len == 0explicitly (a zero-length code is empty, store 0). Also add a symmetric guard forlen >= 128in the other branch, whereout2 << (128 - len)would shift by>= 64(such codes do not fit in 128 bits anyway).Context
This was found while integrating SZ3 into ClickHouse, where compressing a column of constant floating-point values reliably triggered the crash on a UBSan build: ClickHouse/ClickHouse#108788