mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(syntax): prevent creating invalid u32 IDs (#4675)
Panic if try to create an `AstNodeId`, `ReferenceId`, `ScopeId` or `SymbolId` from a `usize` which can't be stored as a `u32`. Previously we checked for `u32::MAX`, but didn't check for numbers larger than that.
This commit is contained in:
parent
e3abdfa379
commit
9f8f2997c4
4 changed files with 12 additions and 4 deletions
|
|
@ -38,7 +38,9 @@ impl AstNodeId {
|
|||
impl Idx for AstNodeId {
|
||||
#[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 {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,9 @@ pub struct ReferenceId(NonMaxU32);
|
|||
impl Idx for ReferenceId {
|
||||
#[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 {
|
||||
|
|
|
|||
|
|
@ -35,7 +35,9 @@ impl ScopeId {
|
|||
impl Idx for ScopeId {
|
||||
#[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 {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,9 @@ pub struct SymbolId(NonMaxU32);
|
|||
impl Idx for SymbolId {
|
||||
#[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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue