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