From 042afa9fd6b44dcc0472522ac9cc1e54ab25ed32 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 13 Sep 2024 12:15:20 +0000 Subject: [PATCH] fix(syntax): correctly check for valid `RedeclarationId`s (#5759) Previously we truncated `usize` to `u32` and *then* checked validity of the `u32`. Fix that by checking validity *before* truncating. --- crates/oxc_syntax/src/symbol.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/oxc_syntax/src/symbol.rs b/crates/oxc_syntax/src/symbol.rs index 20a932c99..b2eabbac4 100644 --- a/crates/oxc_syntax/src/symbol.rs +++ b/crates/oxc_syntax/src/symbol.rs @@ -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 {