summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-07-02 18:50:25 +0200
committerSven Eden <sven.eden@prydeworx.com>2018-10-29 10:18:24 +0100
commit47e5c14d2dba9e136e1ef0e51ebdb3116e9ecd3c (patch)
tree242c19bd58d3e235d1ec07529f3580601065dde7 /src/basic
parent318d9d4056bde63063921594543e06c3fffaf775 (diff)
parse-util: in parse_permille() check negative earlier
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 8cbc92d5975b603002c3141364a7709a9c66e23a)
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/parse-util.c7
1 files changed, 4 insertions, 3 deletions
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index 3935d8a3e..e25e8212d 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -642,6 +642,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)
@@ -662,15 +664,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;
}