]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: introduce *_to_string_with_check() functions
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 29 Dec 2017 08:03:54 +0000 (17:03 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 1 Jan 2018 17:23:24 +0000 (02:23 +0900)
They are used in later commits.

src/basic/errno-list.h
src/basic/parse-util.c
src/basic/process-util.h
src/basic/securebits-util.h
src/basic/signal-util.h

index 4e9b75a7eafda798d855354e0b4d8c75cb910f65..38beaf96dd80f7ebfe7d626d0054a9a34f40833a 100644 (file)
@@ -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;
+}
index d03f60e01a9e5b4d7d32c5f1568589fc25ef7be3..33f94f3fc2eb128e5e511fc881d879d993975e0d 100644 (file)
@@ -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;
index 1dd62c6d0a99189b1b5600dcddf2ac6f59e6ddd4..b20e527af77a3acbb5b2b8354472e2179a95ae57 100644 (file)
@@ -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);
index aaa192f0a504fa074868202b1f757cbee9b6826c..069d215488f1ff8ccc9c43ff0a3087e4a93688c1 100644 (file)
 
 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);
+}
index 76b239b1fcda10191fa964fdd353e1eda6df4197..f6c3396ebe6d64157ab19353a07f048a9334f593 100644 (file)
@@ -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);
+}