lib/memory: properly handle int overflow in sysTotalMemory

This should fix builds on 32-bit architectures such as arm.

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/212
This commit is contained in:
Aliaksandr Valialkin 2019-10-17 00:49:54 +03:00
parent f752479cb8
commit 6ebf537153

View file

@ -9,12 +9,17 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
) )
const maxInt = int(^uint(0) >> 1)
func sysTotalMemory() int { func sysTotalMemory() int {
var si syscall.Sysinfo_t var si syscall.Sysinfo_t
if err := syscall.Sysinfo(&si); err != nil { if err := syscall.Sysinfo(&si); err != nil {
logger.Panicf("FATAL: error in syscall.Sysinfo: %s", err) logger.Panicf("FATAL: error in syscall.Sysinfo: %s", err)
} }
totalMem := int(si.Totalram) * int(si.Unit) totalMem := maxInt
if uint64(maxInt)/uint64(si.Totalram) > uint64(si.Unit) {
totalMem = int(uint64(si.Totalram) * uint64(si.Unit))
}
// Try determining the amount of memory inside docker container. // Try determining the amount of memory inside docker container.
// See https://stackoverflow.com/questions/42187085/check-mem-limit-within-a-docker-container . // See https://stackoverflow.com/questions/42187085/check-mem-limit-within-a-docker-container .