Repository navigation
Conversation
An estimation-mode image stores n separately from the compactors, and deserialize() took it as given. Compaction keeps the total weight of the retained items, so n must equal it; a corrupt image could otherwise give a sketch whose n disagrees with its items. Both deserializers now check it, and also reject a total weight that overflows. Matches the REQ check in apache/datasketches-java#810. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Coverage Report for CI Build 45058842269Warning Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes. Coverage increased (+0.04%) to 97.088%Details
Uncovered Changes
Coverage Regressions38 previously-covered lines in 2 files lost coverage.
Coverage Stats
💛 - Coveralls |
proost
reviewed
Nov 21, 2026
| for (const auto& compactor: compactors) { | ||
| const uint64_t num_items = compactor.get_num_items(); | ||
| const uint8_t lg_weight = compactor.get_lg_weight(); | ||
| if (num_items > 0 && (lg_weight >= 64 || num_items > ((std::numeric_limits<uint64_t>::max() - total_weight) >> lg_weight))) { |
Member
There was a problem hiding this comment.
shifting 64 or more is undefined behavior in C++. So num_items == 0 case can avoid this guard.
Member
Author
There was a problem hiding this comment.
Right, the guard was skipped for an empty compactor but the shift still ran. Fixed in 997ce6b: empty compactors are skipped before the shift, since they add no weight. The new test section "empty compactors above level 63" appends empty compactors up to level 65; before the fix UBSan reported "shift exponent 64 is too large" there.
An empty compactor skipped the overflow guard but was still shifted by its lg_weight, which can be 64 or more in an image with more than 64 levels: undefined behavior. Empty compactors add no weight, so skip them. Reported by proost. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
An estimation-mode REQ image stores n separately from the compactors, and
deserialize()took it as given. Compaction keeps the total weight of the retained items (restore_counts()relies on this), so n must equal the sum of each compactor's item count times its weight. A corrupt image could otherwise give a sketch whose n disagrees with its items, which skews every rank and quantile.Both deserializers (stream and bytes) now compute the total weight and throw
std::invalid_argumentif n differs from it, or if the total overflows 64 bits. Empty compactors are skipped, since they add no weight and their lg_weight can be 64 or more (suggested by proost). Exact-mode images don't store n (it is the number of items), so nothing changes for them.Tests, added to "deserialize rejects inconsistent compactors": n one above and one below the total weight is rejected from bytes and from a stream, and so is an image with empty compactors up to level 65 (which UBSan flagged before the skip). They fail on master. All REQ tests pass under ASan and UBSan, including the existing valid-image cases with a million items in both rank accuracy modes.
The same check is in apache/datasketches-java#810. This PR is independent of #587 and merges cleanly with it and with #593 and #594.
🤖 Generated with Claude Code