Skip to content

REQ: reject n that isn't the total weight on deserialize - #595

Open
leerho wants to merge 2 commits into
masterfrom
req-deserialize-n
Open

leerho wants to merge 2 commits into
masterfrom
req-deserialize-n

Conversation

@leerho

@leerho leerho commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

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_argument if 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

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>
@coveralls

coveralls commented Nov 20, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 45058842269

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage increased (+0.04%) to 97.088%

Details

  • Coverage increased (+0.04%) from the base build.
  • Patch coverage: 1 uncovered change across 1 file (14 of 15 lines covered, 93.33%).
  • 38 coverage regressions across 2 files.

Uncovered Changes

File Changed Covered %
req/include/req_sketch_impl.hpp 15 14 93.33%

Coverage Regressions

38 previously-covered lines in 2 files lost coverage.

File Lines Losing Coverage Coverage
kll/include/kll_sketch_impl.hpp 30 95.96%
fi/include/frequent_items_sketch_impl.hpp 8 96.99%

Coverage Stats

Coverage Status
Relevant Lines: 17480
Covered Lines: 16971
Line Coverage: 97.09%
Coverage Strength: 1882476.44 hits per line

💛 - Coveralls

@leerho
leerho requested a review from proost November 20, 2026 21:07
Comment thread req/include/req_sketch_impl.hpp Outdated
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))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shifting 64 or more is undefined behavior in C++. So num_items == 0 case can avoid this guard.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@leerho
leerho requested a review from proost November 22, 2026 19:06

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants