Skip to content

Conversation

@IshaanXCoder
Copy link

Closes #7442
Adds overflow protection to ZeroAsciiDenseSparse2dTrieBorrowed::get() using checked arithmetic operations.

Changes

File: utils/zerotrie/src/dense.rs

  1. Matrix indexing now uses checked_mul() and checked_add()
  2. Final value calculation uses checked_add() which perevents overflow when adding offset to row_value_offset

@IshaanXCoder IshaanXCoder requested a review from a team as a code owner January 17, 2026 14:28
@CLAassistant
Copy link

CLAassistant commented Jan 17, 2026

CLA assistant check
All committers have signed the CLA.

@Manishearth Manishearth requested a review from sffc January 18, 2026 05:28
use zerotrie::dense::ZeroAsciiDenseSparse2dTrieOwned;

#[test]
fn test_dense_suffix_count_with_low_frequency_suffixes() {
Copy link
Member

Choose a reason for hiding this comment

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

Did you intend to include these tests in this PR or the other PR?

Copy link
Author

Choose a reason for hiding this comment

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

yess these were to be in the previous PR, my bad

.checked_mul(suffix_count)
.and_then(|v| v.checked_add(column_index))?;
let Some(offset) = self.dense.get(index) else {
unreachable!(
Copy link
Member

Choose a reason for hiding this comment

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

Please use debug_assert!(false as before, instead of unreachable!, so that we don't have a panic compiled into the code.

@@ -0,0 +1,149 @@
// This file is part of ICU4X. For terms of use, please see the file
Copy link
Member

Choose a reason for hiding this comment

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

Please put these tests in dense_test.rs and use check_data for the assertions.

let trie = ZeroAsciiDenseSparse2dTrieOwned::try_from_btree_map_str(&data, b'/').unwrap();
let trie = trie.as_borrowed();

// With empty dense matrix, all lookups should still work via sparse path
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: assert the Debug output of the trie, which encodes the values that went into sparse and the values that went into dense.

@IshaanXCoder
Copy link
Author

Hey @sffc i can see there's 2 fail in the check (semver fails after 8m), looking at the logs, it is due to a pre-existing issue, they're in icu_calendar and icu_locale_core.

@IshaanXCoder
Copy link
Author

@sffc made the required changes, PTAL

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.

Add checked arithmetic to prevent integer overflow in dense trie lookups

3 participants