*/
#include "common/macros.h"
+#include <stdint.h>
#define _LGPL_SOURCE
#include <ctype.h>
#include <fcntl.h>
}
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;
* 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) {
/*
* 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;
}
* 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);
}
* 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);
}