logind-inhibit: introduce inhibit_what_is_valid
authorMike Yuan <me@yhndnzj.com>
Thu, 16 Nov 2023 09:46:56 +0000 (17:46 +0800)
committerMike Yuan <me@yhndnzj.com>
Thu, 16 Nov 2023 09:49:33 +0000 (17:49 +0800)
src/login/logind-inhibit.c
src/login/logind-inhibit.h

index af7879f..1566dab 100644 (file)
@@ -411,7 +411,8 @@ bool manager_is_inhibited(
         bool inhibited = false;
 
         assert(m);
-        assert(w > 0 && w < _INHIBIT_WHAT_MAX);
+        assert(w > 0);
+        assert(w < _INHIBIT_WHAT_MAX);
 
         HASHMAP_FOREACH(i, m->inhibitors) {
                 if (!i->started)
@@ -457,7 +458,7 @@ const char *inhibit_what_to_string(InhibitWhat w) {
             "handle-reboot-key")+1];
         char *p;
 
-        if (w < 0 || w >= _INHIBIT_WHAT_MAX)
+        if (!inhibit_what_is_valid(w))
                 return NULL;
 
         p = buffer;
index 6435d41..c34c225 100644 (file)
@@ -68,6 +68,10 @@ bool inhibitor_is_orphan(Inhibitor *i);
 InhibitWhat manager_inhibit_what(Manager *m, InhibitMode mm);
 bool manager_is_inhibited(Manager *m, InhibitWhat w, InhibitMode mm, dual_timestamp *since, bool ignore_inactive, bool ignore_uid, uid_t uid, Inhibitor **offending);
 
+static inline bool inhibit_what_is_valid(InhibitWhat w) {
+        return w > 0 && w < _INHIBIT_WHAT_MAX;
+}
+
 const char *inhibit_what_to_string(InhibitWhat k);
 int inhibit_what_from_string(const char *s);