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