richardphibel / rpms / systemd

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