summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2017-12-29 09:03:54 +0100
committerYu Watanabe <watanabe.yu+github@gmail.com>2018-01-01 18:23:24 +0100
commit33d12153da4d3a90817cd9099962eb992f0131dd (patch)
tree60a8bd04d04a2eb1142fa7b9fda1b1ebd889c6c8
parentsocket-util: introduce parse_socket_address_bind_ipv6_only_or_bool() (diff)
downloadsystemd-33d12153da4d3a90817cd9099962eb992f0131dd.tar.xz
systemd-33d12153da4d3a90817cd9099962eb992f0131dd.zip
basic: introduce *_to_string_with_check() functions
They are used in later commits.
-rw-r--r--src/basic/errno-list.h4
-rw-r--r--src/basic/parse-util.c3
-rw-r--r--src/basic/process-util.h7
-rw-r--r--src/basic/securebits-util.h8
-rw-r--r--src/basic/signal-util.h7
5 files changed, 28 insertions, 1 deletions
diff --git a/src/basic/errno-list.h b/src/basic/errno-list.h
index 4e9b75a7ea..38beaf96dd 100644
--- a/src/basic/errno-list.h
+++ b/src/basic/errno-list.h
@@ -20,6 +20,7 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include <stdbool.h>
/*
* MAX_ERRNO is defined as 4095 in linux/err.h
* We use the same value here.
@@ -28,3 +29,6 @@
const char *errno_to_name(int id);
int errno_from_name(const char *name);
+static inline bool errno_is_valid(int n) {
+ return n > 0 && n <= ERRNO_MAX;
+}
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index d03f60e01a..33f94f3fc2 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -283,7 +283,8 @@ int parse_errno(const char *t) {
if (r < 0)
return r;
- if (e < 0 || e > ERRNO_MAX)
+ /* 0 is also allowed here */
+ if (!errno_is_valid(e) && e != 0)
return -ERANGE;
return e;
diff --git a/src/basic/process-util.h b/src/basic/process-util.h
index 1dd62c6d0a..b20e527af7 100644
--- a/src/basic/process-util.h
+++ b/src/basic/process-util.h
@@ -137,6 +137,13 @@ static inline bool pid_is_valid(pid_t p) {
return p > 0;
}
+static inline int sched_policy_to_string_alloc_with_check(int n, char **s) {
+ if (!sched_policy_is_valid(n))
+ return -EINVAL;
+
+ return sched_policy_to_string_alloc(n, s);
+}
+
int ioprio_parse_priority(const char *s, int *ret);
pid_t getpid_cached(void);
diff --git a/src/basic/securebits-util.h b/src/basic/securebits-util.h
index aaa192f0a5..069d215488 100644
--- a/src/basic/securebits-util.h
+++ b/src/basic/securebits-util.h
@@ -24,6 +24,14 @@
int secure_bits_to_string_alloc(int i, char **s);
int secure_bits_from_string(const char *s);
+
static inline bool secure_bits_is_valid(int i) {
return ((SECURE_ALL_BITS | SECURE_ALL_LOCKS) & i) == i;
}
+
+static inline int secure_bits_to_string_alloc_with_check(int n, char **s) {
+ if (!secure_bits_is_valid(n))
+ return -EINVAL;
+
+ return secure_bits_to_string_alloc(n, s);
+}
diff --git a/src/basic/signal-util.h b/src/basic/signal-util.h
index 76b239b1fc..f6c3396ebe 100644
--- a/src/basic/signal-util.h
+++ b/src/basic/signal-util.h
@@ -55,3 +55,10 @@ static inline void block_signals_reset(sigset_t *ss) {
static inline bool SIGNAL_VALID(int signo) {
return signo > 0 && signo < _NSIG;
}
+
+static inline const char* signal_to_string_with_check(int n) {
+ if (!SIGNAL_VALID(n))
+ return NULL;
+
+ return signal_to_string(n);
+}