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 7d7d7a7d4e
commit 12c8afc3f2

View file

@ -9,12 +9,17 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
)
const maxInt = int(^uint(0) >> 1)
func sysTotalMemory() int {
var si syscall.Sysinfo_t
if err := syscall.Sysinfo(&si); err != nil {
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.
// See https://stackoverflow.com/questions/42187085/check-mem-limit-within-a-docker-container .