From a4488a7509a96d8b9f0d7793517a217c70ac0361 Mon Sep 17 00:00:00 2001 From: Andreas Rammhold Date: Fri, 29 Sep 2017 00:37:23 +0200 Subject: [PATCH] tree-wide: use IN_SET where possible In addition to the changes from #6933 this handles cases that could be matched with the included cocci file. --- coccinelle/in_set.cocci | 35 +++++++++++++++++++++++++++++++++++ src/basic/hashmap.c | 2 +- src/basic/process-util.c | 3 +-- src/basic/signal-util.c | 2 +- src/basic/socket-util.c | 5 ++--- src/basic/terminal-util.c | 2 +- src/basic/util.c | 2 +- src/shared/clean-ipc.c | 6 +++--- src/shared/utmp-wtmp.c | 2 +- 9 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 coccinelle/in_set.cocci diff --git a/coccinelle/in_set.cocci b/coccinelle/in_set.cocci new file mode 100644 index 000000000..f5ddd3d33 --- /dev/null +++ b/coccinelle/in_set.cocci @@ -0,0 +1,35 @@ +@@ +expression e; +identifier n1, n2, n3, n4, n5, n6; +statement s; +@@ +- e == n1 || e == n2 || e == n3 || e == n4 || e == n5 || e == n6 ++ IN_SET(e, n1, n2, n3, n4, n5, n6) +@@ +expression e; +identifier n1, n2, n3, n4, n5; +statement s; +@@ +- e == n1 || e == n2 || e == n3 || e == n4 || e == n5 ++ IN_SET(e, n1, n2, n3, n4, n5) +@@ +expression e; +identifier n1, n2, n3, n4; +statement s; +@@ +- e == n1 || e == n2 || e == n3 || e == n4 ++ IN_SET(e, n1, n2, n3, n4) +@@ +expression e; +identifier n1, n2, n3; +statement s; +@@ +- e == n1 || e == n2 || e == n3 ++ IN_SET(e, n1, n2, n3) +@@ +expression e; +identifier n, p; +statement s; +@@ +- e == n || e == p ++ IN_SET(e, n, p) diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index 86d648df0..51190b381 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -927,7 +927,7 @@ static bool hashmap_put_robin_hood(HashmapBase *h, unsigned idx, for (distance = 0; ; distance++) { raw_dib = dibs[idx]; - if (raw_dib == DIB_RAW_FREE || raw_dib == DIB_RAW_REHASH) { + if (IN_SET(raw_dib, DIB_RAW_FREE, DIB_RAW_REHASH)) { if (raw_dib == DIB_RAW_REHASH) bucket_move_entry(h, swap, idx, IDX_TMP); diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 2b31cbb2b..22d6f5059 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -692,8 +692,7 @@ int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_cod log_debug("%s succeeded.", name); return status.si_status; - } else if (status.si_code == CLD_KILLED || - status.si_code == CLD_DUMPED) { + } else if (IN_SET(status.si_code, CLD_KILLED, CLD_DUMPED)) { log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status)); return -EPROTO; diff --git a/src/basic/signal-util.c b/src/basic/signal-util.c index 8e83bfcf0..043c71466 100644 --- a/src/basic/signal-util.c +++ b/src/basic/signal-util.c @@ -38,7 +38,7 @@ int reset_all_signal_handlers(void) { for (sig = 1; sig < _NSIG; sig++) { /* These two cannot be caught... */ - if (sig == SIGKILL || sig == SIGSTOP) + if (IN_SET(sig, SIGKILL, SIGSTOP)) continue; /* On Linux the first two RT signals are reserved by diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index 5487d1b05..c32b8d3e1 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -364,8 +364,7 @@ bool socket_address_can_accept(const SocketAddress *a) { assert(a); return - a->type == SOCK_STREAM || - a->type == SOCK_SEQPACKET; + IN_SET(a->type, SOCK_STREAM, SOCK_SEQPACKET); } bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) { @@ -1085,7 +1084,7 @@ ssize_t next_datagram_size_fd(int fd) { l = recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC); if (l < 0) { - if (errno == EOPNOTSUPP || errno == EFAULT) + if (IN_SET(errno, EOPNOTSUPP, EFAULT)) goto fallback; return -errno; diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index d580c6dac..b83e778b2 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -479,7 +479,7 @@ int acquire_terminal( l = read(notify, &buffer, sizeof(buffer)); if (l < 0) { - if (errno == EINTR || errno == EAGAIN) + if (IN_SET(errno, EINTR, EAGAIN)) continue; r = -errno; diff --git a/src/basic/util.c b/src/basic/util.c index 463f6fd12..3dfe1ff65 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -383,7 +383,7 @@ int on_ac_power(void) { device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY); if (device < 0) { - if (errno == ENOENT || errno == ENOTDIR) + if (IN_SET(errno, ENOENT, ENOTDIR)) continue; return -errno; diff --git a/src/shared/clean-ipc.c b/src/shared/clean-ipc.c index 3b4231827..3b1c2bd84 100644 --- a/src/shared/clean-ipc.c +++ b/src/shared/clean-ipc.c @@ -95,7 +95,7 @@ static int clean_sysvipc_shm(uid_t delete_uid, gid_t delete_gid) { if (shmctl(shmid, IPC_RMID, NULL) < 0) { /* Ignore entries that are already deleted */ - if (errno == EIDRM || errno == EINVAL) + if (IN_SET(errno, EIDRM, EINVAL)) continue; ret = log_warning_errno(errno, @@ -147,7 +147,7 @@ static int clean_sysvipc_sem(uid_t delete_uid, gid_t delete_gid) { if (semctl(semid, 0, IPC_RMID) < 0) { /* Ignore entries that are already deleted */ - if (errno == EIDRM || errno == EINVAL) + if (IN_SET(errno, EIDRM, EINVAL)) continue; ret = log_warning_errno(errno, @@ -200,7 +200,7 @@ static int clean_sysvipc_msg(uid_t delete_uid, gid_t delete_gid) { if (msgctl(msgid, IPC_RMID, NULL) < 0) { /* Ignore entries that are already deleted */ - if (errno == EIDRM || errno == EINVAL) + if (IN_SET(errno, EIDRM, EINVAL)) continue; ret = log_warning_errno(errno, diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c index 9750dcd81..fc8548c5b 100644 --- a/src/shared/utmp-wtmp.c +++ b/src/shared/utmp-wtmp.c @@ -234,7 +234,7 @@ int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line if (r < 0) return r; - if (ut_type == LOGIN_PROCESS || ut_type == USER_PROCESS) { + if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) { store.ut_type = LOGIN_PROCESS; r = write_entry_both(&store); if (r < 0) -- 2.30.2