From: Chanwoo Choi Date: Wed, 23 Mar 2022 04:59:10 +0000 (+0900) Subject: util: kernel: Fix overflow with bit operations X-Git-Tag: accepted/tizen/unified/20220323.133748^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0c5633b4fc5fd39698a9f9bea446261b513ba1be;p=platform%2Fcore%2Fsystem%2Fpass.git 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 --- 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; } }