naccyde / rpms / systemd

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