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