dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
bd1529
From 2fd9a21a8b6a93c4fb2747839766adca15faa008 Mon Sep 17 00:00:00 2001
bd1529
From: Lennart Poettering <lennart@poettering.net>
bd1529
Date: Thu, 14 Nov 2019 14:49:40 +0100
bd1529
Subject: [PATCH] parse-util: sometimes it is useful to check if a string is a
bd1529
 valid integer, but not actually parse it
bd1529
bd1529
(cherry picked from commit 22810041c2200fe72b0e0c985d0e404f8b80f9e2)
bd1529
bd1529
Related: #1848373
bd1529
---
bd1529
 src/basic/parse-util.c | 34 ++++++++++++++++++++--------------
bd1529
 1 file changed, 20 insertions(+), 14 deletions(-)
bd1529
bd1529
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
bd1529
index 6becf85878..056e56765e 100644
bd1529
--- a/src/basic/parse-util.c
bd1529
+++ b/src/basic/parse-util.c
bd1529
@@ -383,7 +383,6 @@ int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
bd1529
         unsigned long l;
bd1529
 
bd1529
         assert(s);
bd1529
-        assert(ret_u);
bd1529
         assert(base <= 16);
bd1529
 
bd1529
         /* strtoul() is happy to parse negative values, and silently
bd1529
@@ -407,7 +406,9 @@ int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
bd1529
         if ((unsigned long) (unsigned) l != l)
bd1529
                 return -ERANGE;
bd1529
 
bd1529
-        *ret_u = (unsigned) l;
bd1529
+        if (ret_u)
bd1529
+                *ret_u = (unsigned) l;
bd1529
+
bd1529
         return 0;
bd1529
 }
bd1529
 
bd1529
@@ -416,7 +417,6 @@ int safe_atoi(const char *s, int *ret_i) {
bd1529
         long l;
bd1529
 
bd1529
         assert(s);
bd1529
-        assert(ret_i);
bd1529
 
bd1529
         errno = 0;
bd1529
         l = strtol(s, &x, 0);
bd1529
@@ -427,7 +427,9 @@ int safe_atoi(const char *s, int *ret_i) {
bd1529
         if ((long) (int) l != l)
bd1529
                 return -ERANGE;
bd1529
 
bd1529
-        *ret_i = (int) l;
bd1529
+        if (ret_i)
bd1529
+                *ret_i = (int) l;
bd1529
+
bd1529
         return 0;
bd1529
 }
bd1529
 
bd1529
@@ -436,7 +438,6 @@ int safe_atollu(const char *s, long long unsigned *ret_llu) {
bd1529
         unsigned long long l;
bd1529
 
bd1529
         assert(s);
bd1529
-        assert(ret_llu);
bd1529
 
bd1529
         s += strspn(s, WHITESPACE);
bd1529
 
bd1529
@@ -449,7 +450,9 @@ int safe_atollu(const char *s, long long unsigned *ret_llu) {
bd1529
         if (*s == '-')
bd1529
                 return -ERANGE;
bd1529
 
bd1529
-        *ret_llu = l;
bd1529
+        if (ret_llu)
bd1529
+                *ret_llu = l;
bd1529
+
bd1529
         return 0;
bd1529
 }
bd1529
 
bd1529
@@ -458,7 +461,6 @@ int safe_atolli(const char *s, long long int *ret_lli) {
bd1529
         long long l;
bd1529
 
bd1529
         assert(s);
bd1529
-        assert(ret_lli);
bd1529
 
bd1529
         errno = 0;
bd1529
         l = strtoll(s, &x, 0);
bd1529
@@ -467,7 +469,9 @@ int safe_atolli(const char *s, long long int *ret_lli) {
bd1529
         if (!x || x == s || *x != 0)
bd1529
                 return -EINVAL;
bd1529
 
bd1529
-        *ret_lli = l;
bd1529
+        if (ret_lli)
bd1529
+                *ret_lli = l;
bd1529
+
bd1529
         return 0;
bd1529
 }
bd1529
 
bd1529
@@ -476,7 +480,6 @@ int safe_atou8(const char *s, uint8_t *ret) {
bd1529
         unsigned long l;
bd1529
 
bd1529
         assert(s);
bd1529
-        assert(ret);
bd1529
 
bd1529
         s += strspn(s, WHITESPACE);
bd1529
 
bd1529
@@ -491,7 +494,8 @@ int safe_atou8(const char *s, uint8_t *ret) {
bd1529
         if ((unsigned long) (uint8_t) l != l)
bd1529
                 return -ERANGE;
bd1529
 
bd1529
-        *ret = (uint8_t) l;
bd1529
+        if (ret)
bd1529
+                *ret = (uint8_t) l;
bd1529
         return 0;
bd1529
 }
bd1529
 
bd1529
@@ -525,7 +529,6 @@ int safe_atoi16(const char *s, int16_t *ret) {
bd1529
         long l;
bd1529
 
bd1529
         assert(s);
bd1529
-        assert(ret);
bd1529
 
bd1529
         errno = 0;
bd1529
         l = strtol(s, &x, 0);
bd1529
@@ -536,7 +539,9 @@ int safe_atoi16(const char *s, int16_t *ret) {
bd1529
         if ((long) (int16_t) l != l)
bd1529
                 return -ERANGE;
bd1529
 
bd1529
-        *ret = (int16_t) l;
bd1529
+        if (ret)
bd1529
+                *ret = (int16_t) l;
bd1529
+
bd1529
         return 0;
bd1529
 }
bd1529
 
bd1529
@@ -546,7 +551,6 @@ int safe_atod(const char *s, double *ret_d) {
bd1529
         double d = 0;
bd1529
 
bd1529
         assert(s);
bd1529
-        assert(ret_d);
bd1529
 
bd1529
         loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
bd1529
         if (loc == (locale_t) 0)
bd1529
@@ -559,7 +563,9 @@ int safe_atod(const char *s, double *ret_d) {
bd1529
         if (!x || x == s || *x != 0)
bd1529
                 return -EINVAL;
bd1529
 
bd1529
-        *ret_d = (double) d;
bd1529
+        if (ret_d)
bd1529
+                *ret_d = (double) d;
bd1529
+
bd1529
         return 0;
bd1529
 }
bd1529