daandemeyer / rpms / systemd

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