Skip to content

Commit f8e6416

Browse files
committed
parse-util: refuse negative load averages with a zero integer part
parse_loadavg_fixed_point() parses both parts of the value with safe_atolu_full(), which refuses negative numbers with ERANGE, except for "-0". Hence "-4000.5" was refused, but "-0.50" was accepted as 0.50 and "1.-0" as 1.00. Refuse the "-" explicitly, like parse_parts_value_*() in percent-util now do.
1 parent 886b17c commit f8e6416

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

‎src/basic/parse-util.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,11 @@ int parse_loadavg_fixed_point(const char *s, loadavg_t *ret) {
816816
if (r < 0)
817817
return r;
818818

819+
/* safe_atolu_full() accepts "-0", hence refuse the "-" explicitly, so that e.g. "-0.50" is not
820+
* taken as 0.50. */
821+
if (strchr(s, '-'))
822+
return -ERANGE;
823+
819824
return store_loadavg_fixed_point(i, f, ret);
820825
}
821826

‎src/test/test-parse-util.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,9 @@ TEST(parse_loadavg_fixed_point) {
864864

865865
ASSERT_ERROR(parse_loadavg_fixed_point("4096.4096", &fp), ERANGE);
866866
ASSERT_ERROR(parse_loadavg_fixed_point("-4000.5", &fp), ERANGE);
867+
ASSERT_ERROR(parse_loadavg_fixed_point("-0.50", &fp), ERANGE);
868+
ASSERT_ERROR(parse_loadavg_fixed_point("-0.00", &fp), ERANGE);
869+
ASSERT_ERROR(parse_loadavg_fixed_point("1.-0", &fp), ERANGE);
867870
ASSERT_ERROR(parse_loadavg_fixed_point("18446744073709551615.5", &fp), ERANGE);
868871

869872
/* Neither part may be large enough to overflow the shift by LOADAVG_PRECISION_BITS. Derive the

0 commit comments

Comments
 (0)