remove possible unsigned integer overflow for return value 27/243227/4
authorSeung-Woo Kim <sw0312.kim@samsung.com>
Fri, 4 Sep 2020 05:57:50 +0000 (14:57 +0900)
committerSeung-Woo Kim <sw0312.kim@samsung.com>
Fri, 4 Sep 2020 09:41:39 +0000 (18:41 +0900)
Static analysis tool warns about possible overflowed return value.
During calculation, check the possible overflow for return value.

Change-Id: I676a28670249ffc10462254d60d22a8859498ec4
Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
memps.c

diff --git a/memps.c b/memps.c
index c54b007..49e7035 100644 (file)
--- a/memps.c
+++ b/memps.c
@@ -25,6 +25,7 @@
 #include <sys/types.h>
 #include <sys/vfs.h>
 #include <linux/limits.h>
+#include <limits.h>
 
 #include <ctype.h>
 #include <stddef.h>
@@ -398,10 +399,14 @@ static unsigned total_gem_memory(void)
                return 0;
        }
 
-       while (fgets(line, BUF_MAX, gem_fp) != NULL)
+       while (fgets(line, BUF_MAX, gem_fp) != NULL) {
                if (sscanf(line, "%d %d %d %d\n",
-                   &name, &size, &handles, &refcount) == 4)
-                       total_gem_mem += size;
+                   &name, &size, &handles, &refcount) == 4) {
+                       if (total_gem_mem <= UINT_MAX - size) {
+                               total_gem_mem += size;
+                       }
+               }
+       }
        fclose(gem_fp);
 
        return total_gem_mem;