common: cache total RAM as `uint64_t`
What changed, and why it matters
This is a small code-quality change in Bitcoin Core that changes how the program remembers how much RAM a computer has. It now stores the RAM amount as a 64-bit unsigned integer and caches the value once, instead of repeatedly detecting it and clamping it to a smaller 32-bit size. The change removes a potential source of inconsistent cache-size decisions on systems with more than about 4 GB of RAM, but it is not a fix for an active security vulnerability.
Treat as a routine hardening/correctness improvement. Review downstream callers of TryGetTotalRam() to ensure they handle uint64_t safely and do not truncate the value back to a smaller type. No urgent deployment action is required.
Security signals we found
Integer type widened from size_t to uint64_t to avoid clamping
Removed explicit clamp to std::numeric_limits<size_t>::max()
Value is now cached once to prevent TOCTOU-style inconsistency between cache selection and warning logic
Change touches memory sizing code used by database cache configuration
Evidence from the diff
The commit refactors TryGetTotalRam() to return std::optional
Changed components
src/common/system.cppsrc/common/system.hDatabase cache sizing logicOversized cache warning logicInspect captured patch +9 / −14
diff --git a/src/common/system.cpp b/src/common/system.cpp
index 1b1bff17..e61a4a49 100644
--- a/src/common/system.cpp
+++ b/src/common/system.cpp
@@ -25,8 +25,6 @@
#include <malloc.h>
#endif
-#include <algorithm>
-#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <locale>
@@ -111,20 +109,17 @@ int GetNumCores()
return std::thread::hardware_concurrency();
}
-std::optional<size_t> TryGetTotalRam()
+std::optional<uint64_t> TryGetTotalRam()
{
- [[maybe_unused]] auto clamp{[](uint64_t v) { return size_t(std::min(v, uint64_t{std::numeric_limits<size_t>::max()})); }};
+ static const auto total_ram{[]() -> std::optional<uint64_t> {
#ifdef WIN32
- if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return clamp(m.ullTotalPhys);
-#elif defined(__APPLE__) || \
- defined(__FreeBSD__) || \
- defined(__NetBSD__) || \
- defined(__OpenBSD__) || \
- defined(__illumos__) || \
- defined(__linux__)
- if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return clamp(1ULL * p * s);
+ if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return m.ullTotalPhys;
+#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__illumos__) || defined(__linux__)
+ if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return 1ULL * p * s;
#endif
- return std::nullopt;
+ return std::nullopt;
+ }()};
+ return total_ram;
}
namespace {
diff --git a/src/common/system.h b/src/common/system.h
index e0d1b917..52821b7c 100644
--- a/src/common/system.h
+++ b/src/common/system.h
@@ -35,6 +35,6 @@ int GetNumCores();
/**
* Return the total RAM available on the current system, if detectable.
*/
-std::optional<size_t> TryGetTotalRam();
+std::optional<uint64_t> TryGetTotalRam();
#endif // BITCOIN_COMMON_SYSTEM_H
Why this scored 19/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.