diff --git a/src/prim/unix/prim.c b/src/prim/unix/prim.c index da2e2902..f9f38f4e 100644 --- a/src/prim/unix/prim.c +++ b/src/prim/unix/prim.c @@ -142,8 +142,9 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config ) config->alloc_granularity = (size_t)psize; #if defined(_SC_PHYS_PAGES) long pphys = sysconf(_SC_PHYS_PAGES); - if (pphys > 0 && (size_t)pphys < (SIZE_MAX/(size_t)psize)) { - config->physical_memory_in_kib = (size_t)pphys * ((size_t)psize / MI_KiB); + const size_t psize_in_kib = (size_t)psize / MI_KiB; + if (psize_in_kib > 0 && pphys > 0 && (size_t)pphys <= (SIZE_MAX/psize_in_kib)) { + config->physical_memory_in_kib = (size_t)pphys * psize_in_kib; } #endif } diff --git a/src/prim/windows/prim.c b/src/prim/windows/prim.c index a248e934..12a7a5d9 100644 --- a/src/prim/windows/prim.c +++ b/src/prim/windows/prim.c @@ -154,7 +154,7 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config ) if (pGetPhysicallyInstalledSystemMemory != NULL) { ULONGLONG memInKiB = 0; if ((*pGetPhysicallyInstalledSystemMemory)(&memInKiB)) { - if (memInKiB > 0 && memInKiB < (SIZE_MAX / MI_KiB)) { + if (memInKiB > 0 && memInKiB <= SIZE_MAX) { config->physical_memory_in_kib = memInKiB; } }