teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

Blame SOURCES/0460-basic-parse-util-add-safe_atoux64.patch

bd1529
From bd47a98d3ce2c5e1d74deb7bc384e416a9070b96 Mon Sep 17 00:00:00 2001
bd1529
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
bd1529
Date: Thu, 9 Apr 2020 11:18:26 +0200
bd1529
Subject: [PATCH] basic/parse-util: add safe_atoux64()
bd1529
bd1529
(cherry picked from commit ce51632a357d347737bf40d3817df331cd8874cb)
bd1529
bd1529
Related: #1848373
bd1529
---
bd1529
 src/basic/parse-util.c     |  4 ++--
bd1529
 src/basic/parse-util.h     | 12 +++++++++++-
bd1529
 src/test/test-parse-util.c | 39 ++++++++++++++++++++++++++++++++++++++
bd1529
 3 files changed, 52 insertions(+), 3 deletions(-)
bd1529
bd1529
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
bd1529
index 056e56765e..67056c0434 100644
bd1529
--- a/src/basic/parse-util.c
bd1529
+++ b/src/basic/parse-util.c
bd1529
@@ -433,7 +433,7 @@ int safe_atoi(const char *s, int *ret_i) {
bd1529
         return 0;
bd1529
 }
bd1529
 
bd1529
-int safe_atollu(const char *s, long long unsigned *ret_llu) {
bd1529
+int safe_atollu_full(const char *s, unsigned base, long long unsigned *ret_llu) {
bd1529
         char *x = NULL;
bd1529
         unsigned long long l;
bd1529
 
bd1529
@@ -442,7 +442,7 @@ int safe_atollu(const char *s, long long unsigned *ret_llu) {
bd1529
         s += strspn(s, WHITESPACE);
bd1529
 
bd1529
         errno = 0;
bd1529
-        l = strtoull(s, &x, 0);
bd1529
+        l = strtoull(s, &x, base);
bd1529
         if (errno > 0)
bd1529
                 return -errno;
bd1529
         if (!x || x == s || *x != 0)
bd1529
diff --git a/src/basic/parse-util.h b/src/basic/parse-util.h
bd1529
index 1fc1af7615..8a49257050 100644
bd1529
--- a/src/basic/parse-util.h
bd1529
+++ b/src/basic/parse-util.h
bd1529
@@ -33,7 +33,6 @@ static inline int safe_atou(const char *s, unsigned *ret_u) {
bd1529
 }
bd1529
 
bd1529
 int safe_atoi(const char *s, int *ret_i);
bd1529
-int safe_atollu(const char *s, unsigned long long *ret_u);
bd1529
 int safe_atolli(const char *s, long long int *ret_i);
bd1529
 
bd1529
 int safe_atou8(const char *s, uint8_t *ret);
bd1529
@@ -64,6 +63,12 @@ static inline int safe_atoi32(const char *s, int32_t *ret_i) {
bd1529
         return safe_atoi(s, (int*) ret_i);
bd1529
 }
bd1529
 
bd1529
+int safe_atollu_full(const char *s, unsigned base, long long unsigned *ret_llu);
bd1529
+
bd1529
+static inline int safe_atollu(const char *s, long long unsigned *ret_llu) {
bd1529
+        return safe_atollu_full(s, 0, ret_llu);
bd1529
+}
bd1529
+
bd1529
 static inline int safe_atou64(const char *s, uint64_t *ret_u) {
bd1529
         assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
bd1529
         return safe_atollu(s, (unsigned long long*) ret_u);
bd1529
@@ -74,6 +79,11 @@ static inline int safe_atoi64(const char *s, int64_t *ret_i) {
bd1529
         return safe_atolli(s, (long long int*) ret_i);
bd1529
 }
bd1529
 
bd1529
+static inline int safe_atoux64(const char *s, uint64_t *ret) {
bd1529
+        assert_cc(sizeof(int64_t) == sizeof(long long unsigned));
bd1529
+        return safe_atollu_full(s, 16, (long long unsigned*) ret);
bd1529
+}
bd1529
+
bd1529
 #if LONG_MAX == INT_MAX
bd1529
 static inline int safe_atolu(const char *s, unsigned long *ret_u) {
bd1529
         assert_cc(sizeof(unsigned long) == sizeof(unsigned));
bd1529
diff --git a/src/test/test-parse-util.c b/src/test/test-parse-util.c
bd1529
index e9aef5e882..8b182d9bdc 100644
bd1529
--- a/src/test/test-parse-util.c
bd1529
+++ b/src/test/test-parse-util.c
bd1529
@@ -561,6 +561,44 @@ static void test_safe_atoi64(void) {
bd1529
         assert_se(r == -EINVAL);
bd1529
 }
bd1529
 
bd1529
+static void test_safe_atoux64(void) {
bd1529
+        int r;
bd1529
+        uint64_t l;
bd1529
+
bd1529
+        r = safe_atoux64("12345", &l);
bd1529
+        assert_se(r == 0);
bd1529
+        assert_se(l == 0x12345);
bd1529
+
bd1529
+        r = safe_atoux64("  12345", &l);
bd1529
+        assert_se(r == 0);
bd1529
+        assert_se(l == 0x12345);
bd1529
+
bd1529
+        r = safe_atoux64("0x12345", &l);
bd1529
+        assert_se(r == 0);
bd1529
+        assert_se(l == 0x12345);
bd1529
+
bd1529
+        r = safe_atoux64("18446744073709551617", &l);
bd1529
+        assert_se(r == -ERANGE);
bd1529
+
bd1529
+        r = safe_atoux64("-1", &l);
bd1529
+        assert_se(r == -ERANGE);
bd1529
+
bd1529
+        r = safe_atoux64("  -1", &l);
bd1529
+        assert_se(r == -ERANGE);
bd1529
+
bd1529
+        r = safe_atoux64("junk", &l);
bd1529
+        assert_se(r == -EINVAL);
bd1529
+
bd1529
+        r = safe_atoux64("123x", &l);
bd1529
+        assert_se(r == -EINVAL);
bd1529
+
bd1529
+        r = safe_atoux64("12.3", &l);
bd1529
+        assert_se(r == -EINVAL);
bd1529
+
bd1529
+        r = safe_atoux64("", &l);
bd1529
+        assert_se(r == -EINVAL);
bd1529
+}
bd1529
+
bd1529
 static void test_safe_atod(void) {
bd1529
         int r;
bd1529
         double d;
bd1529
@@ -836,6 +874,7 @@ int main(int argc, char *argv[]) {
bd1529
         test_safe_atoux16();
bd1529
         test_safe_atou64();
bd1529
         test_safe_atoi64();
bd1529
+        test_safe_atoux64();
bd1529
         test_safe_atod();
bd1529
         test_parse_percent();
bd1529
         test_parse_percent_unbounded();