refactor: Run ShouldWarnOversizedDbCache calculation in u64
What changed, and why it matters
This is a small internal cleanup change in Bitcoin Core. It changes a helper function that decides whether to warn users about an oversized database cache so that it always uses 64-bit unsigned integers instead of the platform-dependent `size_t` type. The tests are simplified accordingly. There is no user-facing behavior change and no security fix.
No security action needed. Treat as a normal code-quality/refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors ShouldWarnOversizedDbCache in src/node/caches.h to take uint64_t parameters instead of size_t, and updates the cap local variable to uint64_t. This aligns the function with the existing MiB/GiB literal operators and removes conditional 64-bit-only test blocks (if constexpr (SIZE_MAX == UINT64_MAX)). The test values are also expressed in GiB where appropriate. The functional logic (cap = DEFAULT_DB_CACHE when total RAM < 2 GiB, else 75% of total RAM) is unchanged.
Changed components
src/node/caches.hsrc/test/caches_tests.cppsrc/test/system_ram_tests.cppInspect captured patch +18 / −25
diff --git a/src/node/caches.h b/src/node/caches.h
index 483521df..f4143bed 100644
--- a/src/node/caches.h
+++ b/src/node/caches.h
@@ -29,9 +29,9 @@ struct CacheSizes {
kernel::CacheSizes kernel;
};
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
-constexpr bool ShouldWarnOversizedDbCache(size_t dbcache, size_t total_ram) noexcept
+constexpr bool ShouldWarnOversizedDbCache(uint64_t dbcache, uint64_t total_ram) noexcept
{
- const size_t cap{(total_ram < 2_GiB) ? DEFAULT_DB_CACHE : (total_ram / 100) * 75};
+ const uint64_t cap{(total_ram < 2_GiB) ? DEFAULT_DB_CACHE : (total_ram / 100) * 75};
return dbcache > cap;
}
diff --git a/src/test/caches_tests.cpp b/src/test/caches_tests.cpp
index bdca1d40..69c42f0b 100644
--- a/src/test/caches_tests.cpp
+++ b/src/test/caches_tests.cpp
@@ -22,23 +22,21 @@ BOOST_AUTO_TEST_CASE(oversized_dbcache_warning)
BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/1500_MiB, /*total_ram=*/2_GiB)); // Under cap
BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/1600_MiB, /*total_ram=*/2_GiB)); // Over cap
- if constexpr (SIZE_MAX == UINT64_MAX) {
- // 4 GiB RAM - cap is 75%
- BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/2500_MiB, /*total_ram=*/4_GiB)); // Under cap
- BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/3500_MiB, /*total_ram=*/4_GiB)); // Over cap
-
- // 8 GiB RAM - cap is 75%
- BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/6000_MiB, /*total_ram=*/8_GiB)); // Under cap
- BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/7000_MiB, /*total_ram=*/8_GiB)); // Over cap
-
- // 16 GiB RAM - cap is 75%
- BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/10'000_MiB, /*total_ram=*/16_GiB)); // Under cap
- BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/15'000_MiB, /*total_ram=*/16_GiB)); // Over cap
-
- // 32 GiB RAM - cap is 75%
- BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/20'000_MiB, /*total_ram=*/32_GiB)); // Under cap
- BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/30'000_MiB, /*total_ram=*/32_GiB)); // Over cap
- }
+ // 4 GiB RAM - cap is 75%
+ BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/2500_MiB, /*total_ram=*/4_GiB)); // Under cap
+ BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/3500_MiB, /*total_ram=*/4_GiB)); // Over cap
+
+ // 8 GiB RAM - cap is 75%
+ BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/6000_MiB, /*total_ram=*/8_GiB)); // Under cap
+ BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/7000_MiB, /*total_ram=*/8_GiB)); // Over cap
+
+ // 16 GiB RAM - cap is 75%
+ BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/10_GiB, /*total_ram=*/16_GiB)); // Under cap
+ BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/15_GiB, /*total_ram=*/16_GiB)); // Over cap
+
+ // 32 GiB RAM - cap is 75%
+ BOOST_CHECK(!ShouldWarnOversizedDbCache(/*dbcache=*/20_GiB, /*total_ram=*/32_GiB)); // Under cap
+ BOOST_CHECK( ShouldWarnOversizedDbCache(/*dbcache=*/30_GiB, /*total_ram=*/32_GiB)); // Over cap
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/system_ram_tests.cpp b/src/test/system_ram_tests.cpp
index 8ad62264..4eb4e1e3 100644
--- a/src/test/system_ram_tests.cpp
+++ b/src/test/system_ram_tests.cpp
@@ -21,12 +21,7 @@ BOOST_AUTO_TEST_CASE(total_ram)
}
BOOST_CHECK_GE(*total, 1000_MiB);
-
- if constexpr (SIZE_MAX == UINT64_MAX) {
- // Upper bound check only on 64-bit: 32-bit systems can reasonably have max memory,
- // but extremely large values on 64-bit likely indicate detection errors
- BOOST_CHECK_LT(*total, 10'000'000_MiB); // >10 TiB memory is unlikely
- }
+ BOOST_CHECK_LT(*total, 10'000_GiB); // ~10 TiB memory is unlikely
}
BOOST_AUTO_TEST_SUITE_END()
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.