From 5aa42d2fd0235c1c40063a00edd2eb409acd6c52 Mon Sep 17 00:00:00 2001 From: jeff washington Date: Thu, 3 Aug 2023 19:18:12 -0500 Subject: [PATCH] fix typecast --- runtime/src/read_only_accounts_cache.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/runtime/src/read_only_accounts_cache.rs b/runtime/src/read_only_accounts_cache.rs index 10e77ddb714a8e..b4f617f8d96704 100644 --- a/runtime/src/read_only_accounts_cache.rs +++ b/runtime/src/read_only_accounts_cache.rs @@ -38,9 +38,9 @@ impl ReadOnlyAccountCacheEntry { last_time: Self::timestamp(), } } - /// lower bits of current timestamp. We don't need higher bits and u32 fits with Index u32 + /// lower bits of current timestamp. We don't need higher bits and u32 packs with Index u32 in `ReadOnlyAccountCacheEntry` fn timestamp() -> u32 { - (timestamp() % (u32::MAX as u64 + 1)) as u32 + timestamp() as u32 } /// ms since `last_time` timestamp fn ms_since_last_time(&self) -> u32 { @@ -285,6 +285,18 @@ mod tests { /// tests like to deterministically update lru always const READ_ONLY_CACHE_MS_TO_SKIP_LRU_UPDATE_FOR_TESTS: u32 = 0; + #[test] + fn test_overflow() { + solana_logger::setup(); + let l = u32::MAX as u64; + let l = l + 3; + log::error!("{}", (l % (u32::MAX as u64 + 1)) as u32); + log::error!("{}", l as u32); + let l = 3; + log::error!("{}", (l % (u32::MAX as u64 + 1)) as u32); + log::error!("{}", l as u32); + } + #[test] fn test_read_only_accounts_cache_random() { const SEED: [u8; 32] = [0xdb; 32];