Skip to content

Commit

Permalink
parse-util: in parse_permille() check negative earlier
Browse files Browse the repository at this point in the history
If 'v' is negative, it's wrong to add the decimal to it, as we'd
actually need to subtract it in this case. But given that we don't want
to allow negative vaues anyway, simply check earlier whether what we
have parsed so far was negative, and react to that before adding the
decimal to it.

(cherry picked from commit 8cbc92d)

Related: #2178179
  • Loading branch information
poettering authored and systemd-rhel-bot committed Jun 14, 2023
1 parent 30cbb55 commit cb99c3e
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/basic/parse-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,8 @@ int parse_permille_unbounded(const char *p) {
r = safe_atoi(n, &v);
if (r < 0)
return r;
if (v < 0)
return -ERANGE;
} else {
pc = endswith(p, "%");
if (!pc)
Expand All @@ -748,15 +750,14 @@ int parse_permille_unbounded(const char *p) {
r = safe_atoi(n, &v);
if (r < 0)
return r;
if (v < 0)
return -ERANGE;
if (v > (INT_MAX - q) / 10)
return -ERANGE;

v = v * 10 + q;
}

if (v < 0)
return -ERANGE;

return v;
}

Expand Down

0 comments on commit cb99c3e

Please sign in to comment.