mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
fix(syntax): fix unsound use of NonZeroU32 (#4466)
`NonZeroU32::new_unchecked(idx as u32 + 1)` is unsound because if `idx == u32::MAX`, `idx + 1` wraps around back to zero. So unfortunately we need to use the checked version `NonZeroU32::new(idx as u32 + 1).unwrap()` to avoid UB in this edge case.
This commit is contained in:
1 parent
81384f5059
commit
82ba2a073f
2 files changed
+9
-9
No files matched your search
@@ -13,9 +13,9 @@ pub struct ReferenceId(NonZeroU32);
|
||||
impl Idx for ReferenceId {
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
fn from_usize(idx: usize) -> Self {
|
||||
// SAFETY: + 1 is always non-zero.
|
||||
|
||||
unsafe { Self(NonZeroU32::new_unchecked(idx as u32 + 1)) }
|
||||
// NB: We can't use `NonZeroU32::new_unchecked(idx as u32 + 1)`
|
||||
// because if `idx == u32::MAX`, `+ 1` would make it wrap around back to 0
|
||||
Self(NonZeroU32::new(idx as u32 + 1).unwrap())
|
||||
}
|
||||
|
||||
fn index(self) -> usize {
|
||||
|
||||
@@ -13,9 +13,9 @@ pub struct SymbolId(NonZeroU32);
|
||||
impl Idx for SymbolId {
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
fn from_usize(idx: usize) -> Self {
|
||||
// SAFETY: + 1 is always non-zero.
|
||||
|
||||
unsafe { Self(NonZeroU32::new_unchecked(idx as u32 + 1)) }
|
||||
// NB: We can't use `NonZeroU32::new_unchecked(idx as u32 + 1)`
|
||||
// because if `idx == u32::MAX`, `+ 1` would make it wrap around back to 0
|
||||
Self(NonZeroU32::new(idx as u32 + 1).unwrap())
|
||||
}
|
||||
|
||||
fn index(self) -> usize {
|
||||
@@ -30,9 +30,9 @@ pub struct RedeclarationId(NonZeroU32);
|
||||
impl Idx for RedeclarationId {
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
fn from_usize(idx: usize) -> Self {
|
||||
// SAFETY: + 1 is always non-zero.
|
||||
|
||||
unsafe { Self(NonZeroU32::new_unchecked(idx as u32 + 1)) }
|
||||
// NB: We can't use `NonZeroU32::new_unchecked(idx as u32 + 1)`
|
||||
// because if `idx == u32::MAX`, `+ 1` would make it wrap around back to 0
|
||||
Self(NonZeroU32::new(idx as u32 + 1).unwrap())
|
||||
}
|
||||
|
||||
fn index(self) -> usize {
|
||||
|
||||
Reference in new issue
Block a user