Skip to content
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

Truncate keys to u16::MAX in term hashmap #2299

Merged
merged 1 commit into from
Jan 11, 2024
Merged

Truncate keys to u16::MAX in term hashmap #2299

merged 1 commit into from
Jan 11, 2024

Conversation

PSeitz
Copy link
Contributor

@PSeitz PSeitz commented Jan 11, 2024

Truncate keys to u16::MAX, instead e.g. storing 0 bytes for keys with length u16::MAX + 1

The term hashmap has a hidden API contract to only accept terms with lenght up u16::MAX.

@@ -308,6 +312,8 @@ impl SharedArenaHashMap {
if self.is_saturated() {
self.resize();
}
// Limit the key size to u16::MAX
let key = &key[..std::cmp::min(key.len(), u16::MAX as usize)];
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is impl From<u16> for usize, i.e. the cast could be replaced by .into().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer to keep it explicit

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can explicitly write usize::from(u16::MAX) if you prefer while still avoiding the potentially truncating cast.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wish rust would just coerce types that can be safely cast

here I don't think we gain much for proof reading

Truncate keys to u16::MAX, instead e.g. storing 0 bytes for keys with length u16::MAX + 1

The term hashmap has a hidden API contract to only accept terms with lenght up u16::MAX.
@PSeitz PSeitz merged commit 5943ee4 into main Jan 11, 2024
4 checks passed
@PSeitz PSeitz deleted the term_panic branch January 11, 2024 09:19
PSeitz added a commit that referenced this pull request Apr 10, 2024
Truncate keys to u16::MAX, instead e.g. storing 0 bytes for keys with length u16::MAX + 1

The term hashmap has a hidden API contract to only accept terms with lenght up u16::MAX.
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.

3 participants