Repository navigation
KLL: validate n on deserialize - #594
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 45055691797Warning 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.091%Details
Uncovered ChangesNo uncovered changes found. Coverage Regressions8 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
left a comment
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) { |
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.
There was a problem hiding this comment.
Good point, added in 5efc20a. Before it, an image with the first level offset at the capacity and nothing after min and max was accepted as a non-empty sketch with no retained items; the new test section "no retained items" checks it is rejected.
Empty images return before check_n(), and a sketch that is not empty always retains at least one item. An image whose first level offset equals the capacity, with no items after min and max, was accepted as a non-empty sketch with nothing retained. Suggested by proost. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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), a sketch whose n is smaller than the number of items it retains, or a non-empty sketch that retains no items.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": an image with no retained items, 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