8be66a
From 1c8e5070d8a88f35b5577e091de66727fa785ef7 Mon Sep 17 00:00:00 2001
8be66a
From: Lennart Poettering <lennart@poettering.net>
8be66a
Date: Mon, 1 Jun 2020 17:08:38 +0200
8be66a
Subject: [PATCH] parse-util: allow '-0' as alternative to '0' and '+0'
8be66a
8be66a
Let's allow "-0" as alternative to "+0" and "0" when parsing integers,
8be66a
unless the new SAFE_ATO_REFUSE_PLUS_MINUS flag is specified.
8be66a
8be66a
In cases where allowing the +/- syntax shall not be allowed
8be66a
SAFE_ATO_REFUSE_PLUS_MINUS is the right flag to use, but this also means
8be66a
that -0 as only negative integer that fits into an unsigned value should
8be66a
be acceptable if the flag is not specified.
8be66a
8be66a
(cherry picked from commit c78eefc13562a8fc0c22c00a6d3001af89860258)
8be66a
8be66a
Related: #1848373
8be66a
---
8be66a
 src/basic/parse-util.c | 8 ++++----
8be66a
 1 file changed, 4 insertions(+), 4 deletions(-)
8be66a
8be66a
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
8be66a
index 6cc4fc3e57..53d181dd60 100644
8be66a
--- a/src/basic/parse-util.c
8be66a
+++ b/src/basic/parse-util.c
8be66a
@@ -416,7 +416,7 @@ int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
8be66a
                 return -errno;
8be66a
         if (!x || x == s || *x != 0)
8be66a
                 return -EINVAL;
8be66a
-        if (s[0] == '-')
8be66a
+        if (l != 0 && s[0] == '-')
8be66a
                 return -ERANGE;
8be66a
         if ((unsigned long) (unsigned) l != l)
8be66a
                 return -ERANGE;
8be66a
@@ -475,7 +475,7 @@ int safe_atollu_full(const char *s, unsigned base, long long unsigned *ret_llu)
8be66a
                 return -errno;
8be66a
         if (!x || x == s || *x != 0)
8be66a
                 return -EINVAL;
8be66a
-        if (*s == '-')
8be66a
+        if (l != 0 && s[0] == '-')
8be66a
                 return -ERANGE;
8be66a
 
8be66a
         if (ret_llu)
8be66a
@@ -517,7 +517,7 @@ int safe_atou8(const char *s, uint8_t *ret) {
8be66a
                 return -errno;
8be66a
         if (!x || x == s || *x != 0)
8be66a
                 return -EINVAL;
8be66a
-        if (s[0] == '-')
8be66a
+        if (l != 0 && s[0] == '-')
8be66a
                 return -ERANGE;
8be66a
         if ((unsigned long) (uint8_t) l != l)
8be66a
                 return -ERANGE;
8be66a
@@ -554,7 +554,7 @@ int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
8be66a
                 return -errno;
8be66a
         if (!x || x == s || *x != 0)
8be66a
                 return -EINVAL;
8be66a
-        if (s[0] == '-')
8be66a
+        if (l != 0 && s[0] == '-')
8be66a
                 return -ERANGE;
8be66a
         if ((unsigned long) (uint16_t) l != l)
8be66a
                 return -ERANGE;