Thanks to visit codestin.com
Credit goes to GitHub.com

Skip to content

Comments

Use checked arithmetic in dense ZeroTrie#7443

Merged
robertbastian merged 4 commits intounicode-org:mainfrom
IshaanXCoder:feature/add-checked-arithmetic
Jan 22, 2026
Merged

Use checked arithmetic in dense ZeroTrie#7443
robertbastian merged 4 commits intounicode-org:mainfrom
IshaanXCoder:feature/add-checked-arithmetic

Conversation

@IshaanXCoder
Copy link
Contributor

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
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
Contributor 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
Contributor Author

@sffc made the required changes, PTAL

@IshaanXCoder IshaanXCoder force-pushed the feature/add-checked-arithmetic branch from b786821 to 0f7cf5e Compare January 20, 2026 12:19
Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

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

I would appreciate another review from @unicode-org/icu4x-owners since I think this adds branches to a hot path function which maybe doesn't pull its weight. We should avoid building tries that would cause this type of overflow.

@sffc sffc requested a review from a team January 20, 2026 16:11
@robertbastian robertbastian requested review from a team and removed request for a team January 20, 2026 16:56
Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

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

I think the brances are fine, we already have another branch below. These should all just branch predict together.

Maintainer feedback

Co-authored-by: Shane F. Carr <[email protected]>
return None;
}
Some(usize::from(offset) + row_value_offset)
usize::from(offset).checked_add(row_value_offset)
Copy link
Member

Choose a reason for hiding this comment

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

Thought: Maybe this one should debug assert, too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright 👍

@IshaanXCoder
Copy link
Contributor Author

@sffc please review the PRs, made the changes

@robertbastian robertbastian changed the title close 7442 - Added checked arithmetic to prevent integer overflow Use checked arithmetic in dense ZeroTrie Jan 22, 2026
@robertbastian robertbastian merged commit 2ac7eae into unicode-org:main Jan 22, 2026
58 of 61 checks passed
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

5 participants