Skip to content

Commit 0c8069a

Browse files
committed
use physical memory in kib to avoid overflow of size_t (issue microsoft#1010)
1 parent ec4aa62 commit 0c8069a

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/prim/unix/prim.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config )
142142
config->alloc_granularity = (size_t)psize;
143143
#if defined(_SC_PHYS_PAGES)
144144
long pphys = sysconf(_SC_PHYS_PAGES);
145-
if (pphys > 0 && (size_t)pphys < (SIZE_MAX/(size_t)psize)) {
146-
config->physical_memory_in_kib = (size_t)pphys * ((size_t)psize / MI_KiB);
145+
const size_t psize_in_kib = (size_t)psize / MI_KiB;
146+
if (psize_in_kib > 0 && pphys > 0 && (size_t)pphys <= (SIZE_MAX/psize_in_kib)) {
147+
config->physical_memory_in_kib = (size_t)pphys * psize_in_kib;
147148
}
148149
#endif
149150
}

src/prim/windows/prim.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config )
154154
if (pGetPhysicallyInstalledSystemMemory != NULL) {
155155
ULONGLONG memInKiB = 0;
156156
if ((*pGetPhysicallyInstalledSystemMemory)(&memInKiB)) {
157-
if (memInKiB > 0 && memInKiB < (SIZE_MAX / MI_KiB)) {
157+
if (memInKiB > 0 && memInKiB <= SIZE_MAX) {
158158
config->physical_memory_in_kib = memInKiB;
159159
}
160160
}

0 commit comments

Comments
 (0)