Skip to content

Commit

Permalink
fix(syntax): correctly check for valid RedeclarationIds (#5759)
Browse files Browse the repository at this point in the history
Previously we truncated `usize` to `u32` and *then* checked validity of the `u32`. Fix that by checking validity *before* truncating.
  • Loading branch information
overlookmotel committed Sep 13, 2024
1 parent 8ff013a commit 042afa9
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crates/oxc_syntax/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ pub struct RedeclarationId(NonMaxU32);
impl Idx for RedeclarationId {
#[allow(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
Self(NonMaxU32::new(idx as u32).unwrap())
assert!(idx < u32::MAX as usize);
// SAFETY: We just checked `idx` is valid for `NonMaxU32`
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
}

fn index(self) -> usize {
Expand Down

0 comments on commit 042afa9

Please sign in to comment.