Repository navigation
Conversation
update() and merge() cap n at kll_constants::MAX_N since #585, but deserialize() accepted any n, so a corrupt image could produce a sketch with n above the cap, or a non-empty sketch with n = 0, which reports itself as empty. Both deserializers now check n for images that are neither empty nor a single item, and throw std::invalid_argument otherwise. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Coverage Report for CI Build 44804563962Coverage decreased (-0.01%) to 97.04%Details
Uncovered ChangesNo uncovered changes found. Coverage Regressions2 previously-covered lines in 1 file lost coverage.
Coverage Stats
💛 - Coveralls |
Every retained item has a weight of at least 1, so n can't be less than the number of retained items. The check now runs after the levels are validated, for every image that isn't empty. Matches the KLL check in apache/datasketches-java#810. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
proost
approved these changes
Nov 21, 2026
proost
left a comment
Member
There was a problem hiding this comment.
Current logic is correct. I leave minor comment .
| // a sketch that is not empty has n from 1 to kll_constants::MAX_N, the cap that update() and merge() keep, | ||
| // and at least as large as the number of retained items, each of which has a weight of at least 1 | ||
| template<typename T, typename C, typename A> | ||
| void kll_sketch<T, C, A>::check_n(uint64_t n, uint32_t num_retained) { |
Member
There was a problem hiding this comment.
how about adding check "num_retained == 0"?
Because this function is called after empty sketch early returned. So num_retained should be not zero.
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.
Since #585,
update()andmerge()cap n atkll_constants::MAX_N(2^63 - 1, as in datasketches-java), butdeserialize()still accepted any n from the image. A corrupt image could therefore give a sketch with n above the cap, a sketch with retained items and n = 0 (which reports itself as empty), or a sketch whose n is smaller than the number of items it retains.Both deserializers (stream and bytes) now check n once the levels are validated, and throw
std::invalid_argumentif:Empty images don't store n, and single-item images have n = 1 and one item, so nothing changes for them.
Tests, added to "deserialize rejects inconsistent levels": n = 2^63, n = 0 and n = (number of retained items - 1) are rejected from bytes and from a stream, and n = 2^63 - 1 is accepted. The rejection sections fail on master. All KLL tests pass under ASan and UBSan.
The same checks are in apache/datasketches-java#810, which makes Java KLL reject n < 0 and n below the number of retained items.
🤖 Generated with Claude Code