mirror of
https://github.com/morgan9e/systemd
synced 2026-04-14 16:37:19 +09:00
procfs-util: modernize convert_meminfo_value_to_uint64_bytes()
This commit is contained in:
@@ -175,46 +175,34 @@ int procfs_cpu_get_usage(nsec_t *ret) {
|
||||
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)
|
||||
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"))
|
||||
r = extract_first_word(&s, &w, /* separators = */ NULL, /* flags = */ 0);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
return -EINVAL;
|
||||
|
||||
*e = 0;
|
||||
r = safe_atou64(digits, &v);
|
||||
/* Ensure the line ends in "kB" */
|
||||
if (!streq(s, "kB"))
|
||||
return -EINVAL;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user