fundamental: rework IN_SET() to require at least three arguments

If less than three parameters are passed a simple comparison is the
better choice.

Lo and behold this found two pretty bad typos.
This commit is contained in:
Lennart Poettering 2023-01-02 15:22:15 +01:00
parent efa75d7022
commit 4f06325ce9
9 changed files with 15 additions and 15 deletions

View 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')

View 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);

View 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);

View file

@ -278,15 +278,15 @@
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: \

View 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);

View 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 */

View 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. */

View 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));
}

View 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;