ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
8be66a
From e67e29d91a1ef90af545e4130c7b4c4cfde6202a Mon Sep 17 00:00:00 2001
8be66a
From: Lennart Poettering <lennart@poettering.net>
8be66a
Date: Mon, 1 Jun 2020 17:31:51 +0200
8be66a
Subject: [PATCH] parse-util: also parse integers prefixed with 0b and 0o
8be66a
8be66a
Let's adopt Python 3 style 0b and 0x syntaxes, because it makes a ton of
8be66a
sense, in particular in bitmask settings.
8be66a
8be66a
(cherry picked from commit fc80cabcf584a8b486bdff5be0c074fec4059cdc)
8be66a
8be66a
Related: #1848373
8be66a
---
8be66a
 src/basic/parse-util.c | 56 ++++++++++++++++++++++++++++++++++++++----
8be66a
 1 file changed, 51 insertions(+), 5 deletions(-)
8be66a
8be66a
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
8be66a
index 68c156c543..992ea3605b 100644
8be66a
--- a/src/basic/parse-util.c
8be66a
+++ b/src/basic/parse-util.c
8be66a
@@ -17,6 +17,7 @@
8be66a
 #include "parse-util.h"
8be66a
 #include "process-util.h"
8be66a
 #include "string-util.h"
8be66a
+#include "strv.h"
8be66a
 
8be66a
 int parse_boolean(const char *v) {
8be66a
         assert(v);
8be66a
@@ -373,7 +374,32 @@ char *format_bytes(char *buf, size_t l, uint64_t t) {
8be66a
 finish:
8be66a
         buf[l-1] = 0;
8be66a
         return buf;
8be66a
+}
8be66a
+
8be66a
+static const char *mangle_base(const char *s, unsigned *base) {
8be66a
+        const char *k;
8be66a
+
8be66a
+        assert(s);
8be66a
+        assert(base);
8be66a
+
8be66a
+        /* Base already explicitly specified, then don't do anything. */
8be66a
+        if (SAFE_ATO_MASK_FLAGS(*base) != 0)
8be66a
+                return s;
8be66a
 
8be66a
+        /* Support Python 3 style "0b" and 0x" prefixes, because they truly make sense, much more than C's "0" prefix for octal. */
8be66a
+        k = STARTSWITH_SET(s, "0b", "0B");
8be66a
+        if (k) {
8be66a
+                *base = 2 | (*base & SAFE_ATO_ALL_FLAGS);
8be66a
+                return k;
8be66a
+        }
8be66a
+
8be66a
+        k = STARTSWITH_SET(s, "0o", "0O");
8be66a
+        if (k) {
8be66a
+                *base = 8 | (*base & SAFE_ATO_ALL_FLAGS);
8be66a
+                return k;
8be66a
+        }
8be66a
+
8be66a
+        return s;
8be66a
 }
8be66a
 
8be66a
 int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
8be66a
@@ -407,6 +433,8 @@ int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
8be66a
                 return -EINVAL; /* This is particularly useful to avoid ambiguities between C's octal
8be66a
                                  * notation and assumed-to-be-decimal integers with a leading zero. */
8be66a
 
8be66a
+        s = mangle_base(s, &base);
8be66a
+
8be66a
         errno = 0;
8be66a
         l = strtoul(s, &x, SAFE_ATO_MASK_FLAGS(base) /* Let's mask off the flags bits so that only the actual
8be66a
                                                       * base is left */);
8be66a
@@ -426,13 +454,17 @@ int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
8be66a
 }
8be66a
 
8be66a
 int safe_atoi(const char *s, int *ret_i) {
8be66a
+        unsigned base = 0;
8be66a
         char *x = NULL;
8be66a
         long l;
8be66a
 
8be66a
         assert(s);
8be66a
 
8be66a
+        s += strspn(s, WHITESPACE);
8be66a
+        s = mangle_base(s, &base);
8be66a
+
8be66a
         errno = 0;
8be66a
-        l = strtol(s, &x, 0);
8be66a
+        l = strtol(s, &x, base);
8be66a
         if (errno > 0)
8be66a
                 return -errno;
8be66a
         if (!x || x == s || *x != 0)
8be66a
@@ -467,6 +499,8 @@ int safe_atollu_full(const char *s, unsigned base, long long unsigned *ret_llu)
8be66a
             s[0] == '0' && s[1] != 0)
8be66a
                 return -EINVAL;
8be66a
 
8be66a
+        s = mangle_base(s, &base);
8be66a
+
8be66a
         errno = 0;
8be66a
         l = strtoull(s, &x, SAFE_ATO_MASK_FLAGS(base));
8be66a
         if (errno > 0)
8be66a
@@ -483,13 +517,17 @@ int safe_atollu_full(const char *s, unsigned base, long long unsigned *ret_llu)
8be66a
 }
8be66a
 
8be66a
 int safe_atolli(const char *s, long long int *ret_lli) {
8be66a
+        unsigned base = 0;
8be66a
         char *x = NULL;
8be66a
         long long l;
8be66a
 
8be66a
         assert(s);
8be66a
 
8be66a
+        s += strspn(s, WHITESPACE);
8be66a
+        s = mangle_base(s, &base);
8be66a
+
8be66a
         errno = 0;
8be66a
-        l = strtoll(s, &x, 0);
8be66a
+        l = strtoll(s, &x, base);
8be66a
         if (errno > 0)
8be66a
                 return -errno;
8be66a
         if (!x || x == s || *x != 0)
8be66a
@@ -502,15 +540,17 @@ int safe_atolli(const char *s, long long int *ret_lli) {
8be66a
 }
8be66a
 
8be66a
 int safe_atou8(const char *s, uint8_t *ret) {
8be66a
-        char *x = NULL;
8be66a
+        unsigned base = 0;
8be66a
         unsigned long l;
8be66a
+        char *x = NULL;
8be66a
 
8be66a
         assert(s);
8be66a
 
8be66a
         s += strspn(s, WHITESPACE);
8be66a
+        s = mangle_base(s, &base);
8be66a
 
8be66a
         errno = 0;
8be66a
-        l = strtoul(s, &x, 0);
8be66a
+        l = strtoul(s, &x, base);
8be66a
         if (errno > 0)
8be66a
                 return -errno;
8be66a
         if (!x || x == s || *x != 0)
8be66a
@@ -546,6 +586,8 @@ int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
8be66a
             s[0] == '0' && s[1] != 0)
8be66a
                 return -EINVAL;
8be66a
 
8be66a
+        s = mangle_base(s, &base);
8be66a
+
8be66a
         errno = 0;
8be66a
         l = strtoul(s, &x, SAFE_ATO_MASK_FLAGS(base));
8be66a
         if (errno > 0)
8be66a
@@ -564,13 +606,17 @@ int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
8be66a
 }
8be66a
 
8be66a
 int safe_atoi16(const char *s, int16_t *ret) {
8be66a
+        unsigned base = 0;
8be66a
         char *x = NULL;
8be66a
         long l;
8be66a
 
8be66a
         assert(s);
8be66a
 
8be66a
+        s += strspn(s, WHITESPACE);
8be66a
+        s = mangle_base(s, &base);
8be66a
+
8be66a
         errno = 0;
8be66a
-        l = strtol(s, &x, 0);
8be66a
+        l = strtol(s, &x, base);
8be66a
         if (errno > 0)
8be66a
                 return -errno;
8be66a
         if (!x || x == s || *x != 0)