return 0;
}
-int convert_meminfo_value_to_uint64_bytes(const char *word, uint64_t *ret) {
+int convert_meminfo_value_to_uint64_bytes(const char *s, uint64_t *ret) {
_cleanup_free_ char *w = NULL;
- char *digits, *e;
uint64_t v;
- size_t n;
int r;
- assert(word);
+ assert(s);
assert(ret);
- w = strdup(word);
- if (!w)
- return -ENOMEM;
-
- /* Determine length of numeric value */
- n = strspn(w, WHITESPACE);
- digits = w + n;
- n = strspn(digits, DIGITS);
- if (n == 0)
+ r = extract_first_word(&s, &w, /* separators = */ NULL, /* flags = */ 0);
+ if (r < 0)
+ return r;
+ if (r == 0)
return -EINVAL;
- e = digits + n;
- /* Ensure the line ends in " kB" */
- n = strspn(e, WHITESPACE);
- if (n == 0)
- return -EINVAL;
- if (!streq(e + n, "kB"))
+ /* Ensure the line ends in "kB" */
+ if (!streq(s, "kB"))
return -EINVAL;
- *e = 0;
- r = safe_atou64(digits, &v);
+ r = safe_atou64(w, &v);
if (r < 0)
return r;
if (v == UINT64_MAX)
return -EINVAL;
- if (v > UINT64_MAX/1024)
+ if (!MUL_ASSIGN_SAFE(&v, U64_KB))
return -EOVERFLOW;
- *ret = v * 1024U;
+ *ret = v;
return 0;
}