Skip to content

Commit

Permalink
Rollup merge of #91404 - nnethercote:fix-bad-NodeId-limit-checking, r…
Browse files Browse the repository at this point in the history
…=dtolnay

Fix bad `NodeId` limit checking.

`Resolver::next_node_id` converts a `u32` to a `usize` (which is
possibly bigger), does a checked add, and then converts the result back
to a `u32`. The `usize` conversion completely subverts the checked add!

This commit removes the conversion to/from `usize`.
  • Loading branch information
matthiaskrgr authored Dec 1, 2021
2 parents ce197e2 + e7ee823 commit 4f252f1
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,12 +1430,9 @@ impl<'a> Resolver<'a> {
}

pub fn next_node_id(&mut self) -> NodeId {
let next = self
.next_node_id
.as_usize()
.checked_add(1)
.expect("input too large; ran out of NodeIds");
self.next_node_id = ast::NodeId::from_usize(next);
let next =
self.next_node_id.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
self.next_node_id = ast::NodeId::from_u32(next);
self.next_node_id
}

Expand Down

0 comments on commit 4f252f1

Please sign in to comment.