From 99903f24ebf5c1b2b921de051ed7e00c19c0d48d Mon Sep 17 00:00:00 2001 From: Qyriad Date: Sun, 5 May 2024 13:18:59 -0600 Subject: [PATCH] fix integer overflow on i686 with high phys memory sizeof(long) is 4 bytes on i686 GCC. With ~32 GiB of memory and a page size of 4096, there are 7988420 pages. (7988420 * 4096) is bigger than INT32_MAX folks. This has gone unnoticed for 9 years, and only came up thanks to 94ea517db[1] adding integer overflow sensitization checks, which caused this broken code to emit an illegal instruction, crashing Lix the instant the buildsystem ran Lix to generate the docs files. [1]: 94ea517dbe729765b69638190f4bea3f6a632b40 Change-Id: I50bb9ea072aac11b449d79e5d55525887a6e5a99 --- src/libexpr/eval.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index b24f10c24..65f0a7938 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -316,13 +316,14 @@ void initGC() (resident) memory to be allocated. This might be a problem on systems that don't overcommit. */ if (!getEnv("GC_INITIAL_HEAP_SIZE")) { - size_t size = 32 * 1024 * 1024; + int64_t size = 32 * 1024 * 1024; #if HAVE_SYSCONF && defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) - size_t maxSize = 384 * 1024 * 1024; - long pageSize = sysconf(_SC_PAGESIZE); - long pages = sysconf(_SC_PHYS_PAGES); - if (pageSize != -1) + int64_t maxSize = 384 * 1024 * 1024; + int64_t pageSize = sysconf(_SC_PAGESIZE); + int64_t pages = sysconf(_SC_PHYS_PAGES); + if (pageSize != -1) { size = (pageSize * pages) / 4; // 25% of RAM + } if (size > maxSize) size = maxSize; #endif debug("setting initial heap size to %1% bytes", size);