From 79ca724d46918387fba6b2dc484d67390bcbbd56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Thu, 17 Sep 2020 14:37:24 +0200 Subject: [PATCH] Handle the errors from sysconf() call in isc_meminfo_totalphys() isc_meminfo_totalphys() would return invalid memory size when sysconf() call would fail, because ((size_t)-1 * -1) is very large number. --- lib/isc/unix/meminfo.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/isc/unix/meminfo.c b/lib/isc/unix/meminfo.c index 04b01ce177..46cb7d6855 100644 --- a/lib/isc/unix/meminfo.c +++ b/lib/isc/unix/meminfo.c @@ -35,7 +35,14 @@ isc_meminfo_totalphys(void) { #endif /* if defined(CTL_HW) && (defined(HW_PHYSMEM64) || defined(HW_MEMSIZE)) \ * */ #if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) - return ((size_t)(sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE))); + long pages = sysconf(_SC_PHYS_PAGES); + long pagesize = sysconf(_SC_PAGESIZE); + + if (pages == -1 || pagesize == -1) { + return (0); + } + + return ((size_t)pages * pagesize); #endif /* if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) */ return (0); }