]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
parse-util: in parse_permille() check negative earlier
authorLennart Poettering <lennart@poettering.net>
Mon, 2 Jul 2018 16:50:25 +0000 (18:50 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 25 Jul 2018 14:14:45 +0000 (16:14 +0200)
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.

src/basic/parse-util.c

index 6becf85878b07bec3f6f9feffb714624769d87a6..db38f91c832ad31dc68322eb42e367983d8e77aa 100644 (file)
@@ -637,6 +637,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)
@@ -657,15 +659,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;
 }