Repository navigation
Fix high-severity code scanning issues in geo_filters, hash-sorted-map, and bpe - #175
Merged
Merged
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Malformed dictionaries can hang encoding through cyclic failure links or split tables, new unwraps violate CI lint rules, and comparison panics can leave map entries inaccessible.
4 open issues
What changed in this PR
Hardens geo_filters, hash-sorted-map, and bpe against code-scanning issues involving binary decoding, unsafe indexing, and panic-related ownership errors.
Changes:
- Add alignment-aware filter decoding and validate
BitVecpayload lengths. - Delay map entry moves until comparisons succeed and correct endian-dependent control-byte handling.
- Add serialized BPE automaton validation, replace unchecked indexing, and introduce regression tests.
| File | Description |
|---|---|
crates/hash-sorted-map/src/hash_sorted_map.rs |
Delay ownership moves during sorting and test drop behavior after a comparison panic. |
crates/hash-sorted-map/src/group_ops.rs |
Make control-byte decoding portable across byte orders. |
crates/geo_filters/src/diff_count/bitvec.rs |
Correct input lifetimes, handle unaligned data, and reject invalid payload lengths. |
crates/geo_filters/src/diff_count.rs |
Borrow aligned bucket data or copy unaligned data into owned storage. |
crates/bpe/src/prependable_encoder.rs |
Replace unchecked state access with bounds-checked indexing. |
crates/bpe/src/byte_pair_encoding.rs |
Validate automaton bytes, bounds-check token access, and add regression tests. |
crates/bpe/src/appendable_encoder.rs |
Replace unchecked state access with bounds-checked indexing. |
Review effort: Max (auto)
Store BitVec blocks as Cow<'a, [[u8; BYTES_PER_BLOCK]]> and read/write u64 values via u64::from_le_bytes/to_le_bytes so BitVec::from_bytes always borrows directly from the input slice without copying or unsafe code. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Store GeoDiffCount::msb as Cow<[<C::BucketType as IsBucketType>::Bytes]> and decode/encode bucket values via IsBucketType::from_le_bytes and to_le_bytes so GeoDiffCount::from_bytes_with_config and write are zero-copy without requiring BucketType alignment or unsafe code. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Convert bytes[offset..offset + 4] into [u8; 4] via try_into().expect(...) instead of indexing 4 elements individually. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Move msb and lsb comparisons into the Ordering::Equal branches of the preceding match expressions. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
itsibitzi
approved these changes
Nov 4, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.


Summary
Addresses all 6 high-severity
ai-scan/*code-scanning alerts as well as medium-severity alert code-scanning/14 inBitVec::from_bytes:geo_filters:ai-scan/cwe-704,crates/geo_filters/src/diff_count.rs:380) & code-scanning/13 (ai-scan/cwe-704,crates/geo_filters/src/diff_count/bitvec.rs:198):GeoDiffCount::from_bytes_with_configandBitVec::from_bytespreviously transmuted&[u8]to&[C::BucketType]and&[u64]without verifying pointer alignment (and after advancingbufby 1 header byte inBitVec::from_bytes). Introduce a 1-byte-aligned#[repr(C, packed)]Unaligned<T>wrapper incrates/geo_filters/src/config/unaligned.rssoGeoDiffCount::msbandBitVec::blockscan borrow arbitrary byte slices zero-copy (Cow::Borrowed) without alignment UB.ai-scan/cwe-191,crates/geo_filters/src/diff_count/bitvec.rs:190): Check thatBitVec::from_byteshas a non-empty multiple of 8 bytes after the header byte before computingnum_bits, preventing integer underflow on 1-byte inputs.BitVec::from_bytesto its input slice (buf: &'a [u8]).hash-sorted-map:ai-scan/cwe-415,crates/hash-sorted-map/src/hash_sorted_map.rs:220):HashSortedMap::sort_by_hashpreviously movedcur_keyandcur_valout ofMaybeUninitviaassume_init_readbefore theOrd::cmpinsertion-sort loop while leavingctrlmarked occupied, risking a double-drop ifOrd::cmppanicked. Borrowcur_keyin place during comparisons and only movecur_key/cur_val(along with theirctrltags) insideif j < iafter all comparisons for the element succeed.ai-scan/cwe-119,crates/hash-sorted-map/src/group_ops.rs:116): Usefrom_le_bytesingroup_ops(match_tagandcount_occupied) and gateaarch64NEON bitmask extraction ontarget_endian = "little"so slot index bit positions matchtrailing_zeros/leading_zeroson big-endian targets.bpe:ai-scan/cwe-502,crates/bpe/src/byte_pair_encoding.rs:75): Validate serializedDoubleArrayAhoCorasick<u32>buffers inDaacVisitor::visit_bytes(states_lenmultiple of 256, byte lengths,match_kind,num_states, statebase/fail/output_posbounds, and outputlength/parentbounds) before callingDoubleArrayAhoCorasick::deserialize_unchecked.ai-scan/cwe-125,crates/bpe/src/byte_pair_encoding.rs:140): Replace unchecked indexing (get_unchecked) intoken_range,is_valid_token_pair,encode_all_prefixes,AppendableEncoder::push, andPrependableEncoder::pushwith safe bounds-checked indexing.Relates to github/blackbird#17346