on master help sign in
Check for overflow when incrementing the `CacheId` counter
commit03dc31639a01320d1e19cbd8b02289263b59a190 authorNick Fitzgerald <fitzgen@gmail.com> dateMon 1 Apr 2019, 16:29 (8 years ago) parent2ef0923
1 file changed+4−1
src/cached_set.rs+4−1
src/cached_set.rs+4−1
@@ -6,6 +6,7 @@ use crate::{
66use bumpalo::Bump;
77use fxhash::{FxHashMap, FxHashSet};
88use std::sync::atomic::{AtomicUsize, Ordering};
9+use std::u32;
910use wasm_bindgen::prelude::*;
1011
1112static ID_COUNTER: AtomicUsize = AtomicUsize::new(0);
@@ -101,7 +102,9 @@ impl CachedSet {
101102 }
102103
103104 fn next_id(&mut self) -> CacheId {
104− CacheId(ID_COUNTER.fetch_add(1, Ordering::AcqRel) as u32)
105+ let next = ID_COUNTER.fetch_add(1, Ordering::AcqRel) as u32;
106+ let next = if next == u32::MAX { None } else { Some(next) };
107+ CacheId(next.expect_throw("ID_COUNTER overflowed"))
105108 }
106109
107110 pub(crate) fn insert<F>(cx: &mut RenderContext, f: F) -> CacheId