Fix: conversion from KB to bytes overflow on arm32
[lttng-tools.git] / src / common / utils.cpp
index 410f8ec872e92de06da8171a10df72257372be9b..5e116a9ab09513baa6809ed06671954c37156372 100644 (file)
@@ -7,6 +7,7 @@
  */
 
 #include "common/macros.h"
+#include <stdint.h>
 #define _LGPL_SOURCE
 #include <ctype.h>
 #include <fcntl.h>
@@ -1041,7 +1042,7 @@ end:
 }
 
 static
-int read_proc_meminfo_field(const char *field, size_t *value)
+int read_proc_meminfo_field(const char *field, uint64_t *value)
 {
        int ret;
        FILE *proc_meminfo;
@@ -1059,10 +1060,10 @@ int read_proc_meminfo_field(const char *field, size_t *value)
         * field.
         */
        while (!feof(proc_meminfo)) {
-               unsigned long value_kb;
+               uint64_t value_kb;
 
                ret = fscanf(proc_meminfo,
-                               "%" MAX_NAME_LEN_SCANF_IS_A_BROKEN_API "s %lu kB\n",
+                               "%" MAX_NAME_LEN_SCANF_IS_A_BROKEN_API "s %" SCNu64 " kB\n",
                                name, &value_kb);
                if (ret == EOF) {
                        /*
@@ -1079,7 +1080,12 @@ int read_proc_meminfo_field(const char *field, size_t *value)
                         * This number is displayed in kilo-bytes. Return the
                         * number of bytes.
                         */
-                       *value = ((size_t) value_kb) * 1024;
+                       if (value_kb > UINT64_MAX / 1024) {
+                               ERR("Overflow on kb to bytes conversion");
+                               break;
+                       }
+
+                       *value = value_kb * 1024;
                        ret = 0;
                        goto found;
                }
@@ -1098,7 +1104,7 @@ fopen_error:
  * the information in `/proc/meminfo`. The number returned by this function is
  * a best guess.
  */
-int utils_get_memory_available(size_t *value)
+int utils_get_memory_available(uint64_t *value)
 {
        return read_proc_meminfo_field(PROC_MEMINFO_MEMAVAILABLE_LINE, value);
 }
@@ -1107,7 +1113,7 @@ int utils_get_memory_available(size_t *value)
  * Returns the total size of the memory on the system in bytes based on the
  * the information in `/proc/meminfo`.
  */
-int utils_get_memory_total(size_t *value)
+int utils_get_memory_total(uint64_t *value)
 {
        return read_proc_meminfo_field(PROC_MEMINFO_MEMTOTAL_LINE, value);
 }
This page took 0.023837 seconds and 4 git commands to generate.