util: kernel: Fix overflow with bit operations 13/272713/2 accepted/tizen/unified/20220323.133748 submit/tizen/20220323.085026
authorChanwoo Choi <cw00.choi@samsung.com>
Wed, 23 Mar 2022 04:59:10 +0000 (13:59 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Wed, 23 Mar 2022 08:30:40 +0000 (17:30 +0900)
Drop 10 MSB bit and 10 left shift operation without multiply operation.

Change-Id: I9cc0d5a3f131686c49757edfc2565f25e6a1ecef
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
include/util/common.h
src/util/kernel.c

index 9cb9373..48315b8 100644 (file)
@@ -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) ({                     \
index bd797fc..3a9c2d6 100644 (file)
@@ -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;
                }
        }