]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fundamental: rework IN_SET() to require at least three arguments
authorLennart Poettering <lennart@poettering.net>
Mon, 2 Jan 2023 14:22:15 +0000 (15:22 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 2 Jan 2023 16:16:50 +0000 (17:16 +0100)
If less than three parameters are passed a simple comparison is the
better choice.

Lo and behold this found two pretty bad typos.

src/basic/fileio.c
src/core/timer.c
src/core/unit.c
src/fundamental/macro-fundamental.h
src/resolve/resolved-dns-rr.h
src/shared/varlink.c
src/systemctl/systemctl-start-special.c
src/test/test-macro.c
src/tmpfiles/offline-passwd.c

index 0937e58a1527ff20a99076dc018267628ac24c78..3b920418b09ab78801930e7749279142d9fb8800 100644 (file)
@@ -1249,7 +1249,7 @@ typedef enum EndOfLineMarker {
 
 static EndOfLineMarker categorize_eol(char c, ReadLineFlags flags) {
 
-        if (!IN_SET(flags, READ_LINE_ONLY_NUL)) {
+        if (!FLAGS_SET(flags, READ_LINE_ONLY_NUL)) {
                 if (c == '\n')
                         return EOL_TEN;
                 if (c == '\r')
index b6810c8599b836833d32548a8f11703a709efe39..371e42e0aa030014cdeaa6298032f9f49307ee6e 100644 (file)
@@ -864,7 +864,7 @@ static int timer_clean(Unit *u, ExecCleanMask mask) {
         if (t->state != TIMER_DEAD)
                 return -EBUSY;
 
-        if (!IN_SET(mask, EXEC_CLEAN_STATE))
+        if (mask != EXEC_CLEAN_STATE)
                 return -EUNATCH;
 
         r = timer_setup_persistent(t);
index 3b27e6e6d4c8936cb753ae7e72ba11e39eae6251..0e66cae4341ee5293f96f5ca909fbb5b5143fd21 100644 (file)
@@ -5793,7 +5793,7 @@ int unit_clean(Unit *u, ExecCleanMask mask) {
                 return -EBUSY;
 
         state = unit_active_state(u);
-        if (!IN_SET(state, UNIT_INACTIVE))
+        if (state != UNIT_INACTIVE)
                 return -EBUSY;
 
         return UNIT_VTABLE(u)->clean(u, mask);
index db778c7609975150d8deb9ea800d9b92bc1b47c4..9400c389ca9d18bb077a1ec3d3cdf6f0b95c2726 100644 (file)
                                CASE_F_10,CASE_F_9,CASE_F_8,CASE_F_7,CASE_F_6,CASE_F_5,CASE_F_4,CASE_F_3,CASE_F_2,CASE_F_1) \
                    (__VA_ARGS__)
 
-#define IN_SET(x, ...)                                                  \
+#define IN_SET(x, first, ...)                                           \
         ({                                                              \
                 bool _found = false;                                    \
                 /* If the build breaks in the line below, you need to extend the case macros. We use typeof(+x) \
                  * here to widen the type of x if it is a bit-field as this would otherwise be illegal. */      \
-                static const typeof(+x) __assert_in_set[] _unused_ = { __VA_ARGS__ }; \
+                static const typeof(+x) __assert_in_set[] _unused_ = { first, __VA_ARGS__ }; \
                 assert_cc(ELEMENTSOF(__assert_in_set) <= 20);           \
                 switch (x) {                                            \
-                FOR_EACH_MAKE_CASE(__VA_ARGS__)                         \
+                FOR_EACH_MAKE_CASE(first, __VA_ARGS__)                  \
                         _found = true;                                  \
                         break;                                          \
                 default:                                                \
index d558842c0e3d88348a174e9b3cd64b9d28c2c206..024cfb874470f364fad13568307462b803e88fb8 100644 (file)
@@ -320,7 +320,7 @@ ssize_t dns_resource_record_payload(DnsResourceRecord *rr, void **out);
 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceKey*, dns_resource_key_unref);
 
 static inline bool dns_key_is_shared(const DnsResourceKey *key) {
-        return IN_SET(key->type, DNS_TYPE_PTR);
+        return key->type == DNS_TYPE_PTR;
 }
 
 bool dns_resource_key_reduce(DnsResourceKey **a, DnsResourceKey **b);
index 80820fcad214542620d7b6fa0f75bcfdb2d2c42f..8244f599a9a0a86c66160fb2e1637f69ee13aa5f 100644 (file)
@@ -423,7 +423,7 @@ static int varlink_test_disconnect(Varlink *v) {
         /* Similar, if are a client that hasn't written anything yet but the write side is dead, also
          * disconnect. We also explicitly check for POLLHUP here since we likely won't notice the write side
          * being down if we never wrote anything. */
-        if (IN_SET(v->state, VARLINK_IDLE_CLIENT) && (v->write_disconnected || v->got_pollhup))
+        if (v->state == VARLINK_IDLE_CLIENT && (v->write_disconnected || v->got_pollhup))
                 goto disconnect;
 
         /* We are on the server side and still want to send out more replies, but we saw POLLHUP already, and
@@ -1473,7 +1473,7 @@ int varlink_call(
 
         if (v->state == VARLINK_DISCONNECTED)
                 return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected.");
-        if (!IN_SET(v->state, VARLINK_IDLE_CLIENT))
+        if (v->state != VARLINK_IDLE_CLIENT)
                 return varlink_log_errno(v, SYNTHETIC_ERRNO(EBUSY), "Connection busy.");
 
         assert(v->n_pending == 0); /* n_pending can't be > 0 if we are in VARLINK_IDLE_CLIENT state */
index edc907c832d4634ad305dfbfa1d5898070d474d5..fdf2cdfd05117461651848a2a59b0dda02e71431 100644 (file)
@@ -216,7 +216,7 @@ int verb_start_special(int argc, char *argv[], void *userdata) {
 
                         arg_no_block = true;
 
-                } else if (IN_SET(a, ACTION_EXIT))
+                } else if (a == ACTION_EXIT)
                         /* Since exit is so close in behaviour to power-off/reboot, let's also make
                          * it asynchronous, in order to not confuse the user needlessly with unexpected
                          * behaviour. */
index d4f32496b7e71ddecae4b9958a379461a50f16e3..aec1f1ecd42656fa625368f0e9c483f1d3ba6645 100644 (file)
@@ -202,22 +202,22 @@ TEST(ptr_to_int) {
 }
 
 TEST(in_set) {
-        assert_se(IN_SET(1, 1));
+        assert_se(IN_SET(1, 1, 2));
         assert_se(IN_SET(1, 1, 2, 3, 4));
         assert_se(IN_SET(2, 1, 2, 3, 4));
         assert_se(IN_SET(3, 1, 2, 3, 4));
         assert_se(IN_SET(4, 1, 2, 3, 4));
-        assert_se(!IN_SET(0, 1));
+        assert_se(!IN_SET(0, 1, 2));
         assert_se(!IN_SET(0, 1, 2, 3, 4));
 
         struct {
                 unsigned x:3;
         } t = { 1 };
 
-        assert_se(IN_SET(t.x, 1));
+        assert_se(IN_SET(t.x, 1, 2));
         assert_se(IN_SET(t.x, 1, 2, 3, 4));
         assert_se(IN_SET(t.x, 2, 3, 4, 1));
-        assert_se(!IN_SET(t.x, 0));
+        assert_se(!IN_SET(t.x, 0, 2));
         assert_se(!IN_SET(t.x, 2, 3, 4));
 }
 
index f712099b0b6d3c035e54fd7f4fb62df4be281115..4ffed1c10236076a5c9d188defc443c6d01ba5c6 100644 (file)
@@ -62,7 +62,7 @@ static int populate_uid_cache(const char *root, Hashmap **ret) {
                                 return -ENOMEM;
 
                         r = hashmap_put(cache, n, UID_TO_PTR(pw->pw_uid));
-                        if (IN_SET(r, 0 -EEXIST))
+                        if (IN_SET(r, 0, -EEXIST))
                                 continue;
                         if (r < 0)
                                 return r;