Repository navigation
Check header fields on deserialize (Bloom, HLL, density, tuple, VarOpt) #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,11 +128,12 @@ HllArray<A>* HllArray<A>::newHll(const void* bytes, size_t len, const A& allocat | |
| uint32_t numAtCurMin, auxCount; | ||
| std::memcpy(&numAtCurMin, data + hll_constants::CUR_MIN_COUNT_INT, sizeof(int)); | ||
| std::memcpy(&auxCount, data + hll_constants::AUX_COUNT_INT, sizeof(int)); | ||
| checkAuxCount(auxCount, tgtHllType); | ||
|
|
||
| AuxHashMap<A>* auxHashMap = nullptr; | ||
| typedef std::unique_ptr<AuxHashMap<A>, std::function<void(AuxHashMap<A>*)>> aux_hash_map_ptr; | ||
| aux_hash_map_ptr aux_ptr; | ||
| if (auxCount > 0) { // necessarily TgtHllType == HLL_4 | ||
| if (auxCount > 0) { // TgtHllType == HLL_4, see checkAuxCount() | ||
| uint8_t auxLgIntArrSize = data[4]; | ||
| const size_t offset = hll_constants::HLL_BYTE_ARR_START + arrayBytes; | ||
| const uint8_t* auxDataStart = data + offset; | ||
|
|
@@ -200,12 +201,13 @@ HllArray<A>* HllArray<A>::newHll(std::istream& is, const A& allocator) { | |
|
|
||
| const auto numAtCurMin = read<uint32_t>(is); | ||
| const auto auxCount = read<uint32_t>(is); | ||
| checkAuxCount(auxCount, tgtHllType); | ||
| sketch->putNumAtCurMin(numAtCurMin); | ||
|
|
||
| read(is, sketch->hllByteArr_.data(), sketch->getHllByteArrBytes()); | ||
| if (tgtHllType == HLL_8) { checkHll8Registers(sketch->hllByteArr_.data(), sketch->getHllByteArrBytes()); } | ||
|
|
||
| if (auxCount > 0) { // necessarily TgtHllType == HLL_4 | ||
| if (auxCount > 0) { // TgtHllType == HLL_4, see checkAuxCount() | ||
| uint8_t auxLgIntArrSize = listHeader[4]; | ||
| AuxHashMap<A>* auxHashMap = AuxHashMap<A>::deserialize(is, lgK, auxCount, auxLgIntArrSize, comapctFlag, allocator); | ||
| ((Hll4Array<A>*)sketch)->putAuxHashMap(auxHashMap); | ||
|
|
@@ -216,6 +218,14 @@ HllArray<A>* HllArray<A>::newHll(std::istream& is, const A& allocator) { | |
| return sketch_ptr.release(); | ||
| } | ||
|
|
||
| template<typename A> | ||
| void HllArray<A>::checkAuxCount(uint32_t auxCount, target_hll_type tgtHllType) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about adding hllByteArr's token count and auxCount comparison too here? Maybe corrupted image can be different two counts.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, that is worth checking. It is part of a follow-up PR on the HLL readers that I will open after this one, to keep this one small. There, each HLL_4 register holding the aux token must have an aux entry whose value does not fit in the register, and the number of such registers must equal auxCount, in both readers. |
||
| // only HLL_4 has an aux map | ||
| if (auxCount > 0 && tgtHllType != HLL_4) { | ||
| throw std::invalid_argument("Possible corruption: aux entries in a non-HLL_4 image"); | ||
| } | ||
| } | ||
|
|
||
| template<typename A> | ||
| auto HllArray<A>::serialize(bool compact, unsigned header_size_bytes) const -> vector_bytes { | ||
| const size_t sketchSizeBytes = (compact ? getCompactSerializationBytes() : getUpdatableSerializationBytes()) + header_size_bytes; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-empty sketch case, so we should check "num_retained" is not 0 in here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks. A non-empty image with num_retained 0 loaded as a sketch with no levels, and the next update() never returned. Both readers now reject it, with a test (62c4072).