From 0c5633b4fc5fd39698a9f9bea446261b513ba1be Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Wed, 23 Mar 2022 13:59:10 +0900 Subject: [PATCH] util: kernel: Fix overflow with bit operations Drop 10 MSB bit and 10 left shift operation without multiply operation. Change-Id: I9cc0d5a3f131686c49757edfc2565f25e6a1ecef Signed-off-by: Chanwoo Choi --- include/util/common.h | 3 +++ src/util/kernel.c | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/util/common.h b/include/util/common.h index 9cb9373..48315b8 100644 --- a/include/util/common.h +++ b/include/util/common.h @@ -99,6 +99,9 @@ typedef unsigned long long uint64; #ifndef BIT32 #define BIT32(x) (1UL << x) #endif +#ifndef GENMASK64 +#define GENMASK64(h, l) (((~0ULL) << (l)) & (~0ULL >> (64 - 1 - (h)))) +#endif #ifndef container_of #define container_of(ptr, type, member) ({ \ diff --git a/src/util/kernel.c b/src/util/kernel.c index bd797fc..3a9c2d6 100644 --- a/src/util/kernel.c +++ b/src/util/kernel.c @@ -183,8 +183,10 @@ int kernel_get_memory_info(const char *key, u_int64_t *val) while (fgets(buf, BUFF_MAX, fp)) { if (!strncmp(buf, key, strlen(key))) { sscanf(buf, "%*s %"PRIu64, val); - if (strstr(buf, "kB")) - *val *= 1024; + if (strstr(buf, "kB")) { + *val &= GENMASK64(53, 0); + *val <<= 10; + } break; } } -- 2.7.4