summaryrefslogtreecommitdiffstats
path: root/coccinelle
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-06 02:25:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-06 02:25:50 +0000
commit19f4f86bfed21c5326ed2acebe1163f3a83e832b (patch)
treed59b9989ce55ed23693e80974d94c856f1c2c8b1 /coccinelle
parentInitial commit. (diff)
downloadsystemd-19f4f86bfed21c5326ed2acebe1163f3a83e832b.tar.xz
systemd-19f4f86bfed21c5326ed2acebe1163f3a83e832b.zip
Adding upstream version 241.upstream/241upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'coccinelle')
-rw-r--r--coccinelle/bool-cast.cocci12
-rw-r--r--coccinelle/close-above-stdio.cocci36
-rw-r--r--coccinelle/cmp.cocci28
-rw-r--r--coccinelle/const-strlen.cocci10
-rw-r--r--coccinelle/debug-logging.cocci8
-rw-r--r--coccinelle/div-round-up.cocci20
-rw-r--r--coccinelle/dup-fcntl.cocci5
-rw-r--r--coccinelle/empty-or-root.cocci10
-rw-r--r--coccinelle/empty-to-null.cocci5
-rw-r--r--coccinelle/enotsup.cocci4
-rw-r--r--coccinelle/equals-null.cocci14
-rw-r--r--coccinelle/errno.cocci32
-rw-r--r--coccinelle/exit-0.cocci16
-rw-r--r--coccinelle/flags-set.cocci15
-rw-r--r--coccinelle/free_and_replace.cocci15
-rw-r--r--coccinelle/hashmap_free.cocci54
-rw-r--r--coccinelle/htonl.cocci20
-rw-r--r--coccinelle/in_set.cocci54
-rw-r--r--coccinelle/iovec-make.cocci29
-rw-r--r--coccinelle/isempty.cocci60
-rw-r--r--coccinelle/malloc_multiply.cocci20
-rw-r--r--coccinelle/memzero.cocci30
-rw-r--r--coccinelle/mfree.cocci6
-rw-r--r--coccinelle/mfree_return.cocci6
-rw-r--r--coccinelle/no-if-assignments.cocci20
-rw-r--r--coccinelle/not_in_set.cocci54
-rw-r--r--coccinelle/o-ndelay.cocci4
-rw-r--r--coccinelle/reallocarray.cocci20
-rw-r--r--coccinelle/redundant-if.cocci54
-rwxr-xr-xcoccinelle/run-coccinelle.sh27
-rw-r--r--coccinelle/safe_close-no-if.cocci7
-rw-r--r--coccinelle/safe_close.cocci18
-rw-r--r--coccinelle/safe_closedir.cocci27
-rw-r--r--coccinelle/safe_fclose.cocci27
-rw-r--r--coccinelle/strempty.cocci48
-rw-r--r--coccinelle/strjoin.cocci16
-rw-r--r--coccinelle/strjoina.cocci6
-rw-r--r--coccinelle/strv_free.cocci27
-rw-r--r--coccinelle/swap-two.cocci7
-rw-r--r--coccinelle/synthetic-errno.cocci42
-rw-r--r--coccinelle/take-fd.cocci14
-rw-r--r--coccinelle/take-ptr.cocci14
-rw-r--r--coccinelle/while-true.cocci12
-rw-r--r--coccinelle/xsprintf.cocci6
44 files changed, 959 insertions, 0 deletions
diff --git a/coccinelle/bool-cast.cocci b/coccinelle/bool-cast.cocci
new file mode 100644
index 0000000..051ccb9
--- /dev/null
+++ b/coccinelle/bool-cast.cocci
@@ -0,0 +1,12 @@
+@@
+bool b;
+expression y;
+@@
+- b = !!(y);
++ b = y;
+@@
+bool b;
+expression y;
+@@
+- b = !!y;
++ b = y;
diff --git a/coccinelle/close-above-stdio.cocci b/coccinelle/close-above-stdio.cocci
new file mode 100644
index 0000000..44b3b1c
--- /dev/null
+++ b/coccinelle/close-above-stdio.cocci
@@ -0,0 +1,36 @@
+@@
+expression fd;
+@@
+- if (fd > 2)
+- safe_close(fd);
++ safe_close_above_stdio(fd);
+@@
+expression fd;
+@@
+- if (fd > 2)
+- fd = safe_close(fd);
++ fd = safe_close_above_stdio(fd);
+@@
+expression fd;
+@@
+- if (fd >= 3)
+- safe_close(fd);
++ safe_close_above_stdio(fd);
+@@
+expression fd;
+@@
+- if (fd >= 3)
+- fd = safe_close(fd);
++ fd = safe_close_above_stdio(fd);
+@@
+expression fd;
+@@
+- if (fd > STDERR_FILENO)
+- safe_close(fd);
++ safe_close_above_stdio(fd);
+@@
+expression fd;
+@@
+- if (fd > STDERR_FILENO)
+- fd = safe_close(fd);
++ fd = safe_close_above_stdio(fd);
diff --git a/coccinelle/cmp.cocci b/coccinelle/cmp.cocci
new file mode 100644
index 0000000..a34cbe5
--- /dev/null
+++ b/coccinelle/cmp.cocci
@@ -0,0 +1,28 @@
+@@
+expression x, y;
+@@
+- if (x < y)
+- return -1;
+- if (x > y)
+- return 1;
+- return 0;
++ return CMP(x, y);
+@@
+expression x, y;
+@@
+- if (x < y)
+- return -1;
+- else if (x > y)
+- return 1;
+- return 0;
++ return CMP(x, y);
+@@
+expression x, y;
+@@
+- if (x < y)
+- return -1;
+- else if (x > y)
+- return 1;
+- else
+- return 0;
++ return CMP(x, y);
diff --git a/coccinelle/const-strlen.cocci b/coccinelle/const-strlen.cocci
new file mode 100644
index 0000000..38bf9b1
--- /dev/null
+++ b/coccinelle/const-strlen.cocci
@@ -0,0 +1,10 @@
+@@
+constant s;
+@@
+- sizeof(s)-1
++ STRLEN(s)
+@@
+constant s;
+@@
+- strlen(s)
++ STRLEN(s)
diff --git a/coccinelle/debug-logging.cocci b/coccinelle/debug-logging.cocci
new file mode 100644
index 0000000..9084cf7
--- /dev/null
+++ b/coccinelle/debug-logging.cocci
@@ -0,0 +1,8 @@
+@@
+@@
+- _unlikely_(log_get_max_level() >= LOG_DEBUG)
++ DEBUG_LOGGING
+@@
+@@
+- log_get_max_level() >= LOG_DEBUG
++ DEBUG_LOGGING
diff --git a/coccinelle/div-round-up.cocci b/coccinelle/div-round-up.cocci
new file mode 100644
index 0000000..a0c6df9
--- /dev/null
+++ b/coccinelle/div-round-up.cocci
@@ -0,0 +1,20 @@
+@@
+expression x, y;
+@@
+- ((x + y - 1) / y)
++ DIV_ROUND_UP(x, y)
+@@
+expression x, y;
+@@
+- ((x + (y - 1)) / y)
++ DIV_ROUND_UP(x, y)
+@@
+expression x, y;
+@@
+- (x + y - 1) / y
++ DIV_ROUND_UP(x, y)
+@@
+expression x, y;
+@@
+- (x + (y - 1)) / y
++ DIV_ROUND_UP(x, y)
diff --git a/coccinelle/dup-fcntl.cocci b/coccinelle/dup-fcntl.cocci
new file mode 100644
index 0000000..ef13564
--- /dev/null
+++ b/coccinelle/dup-fcntl.cocci
@@ -0,0 +1,5 @@
+@@
+expression fd;
+@@
+- dup(fd)
++ fcntl(fd, F_DUPFD, 3)
diff --git a/coccinelle/empty-or-root.cocci b/coccinelle/empty-or-root.cocci
new file mode 100644
index 0000000..bf2f614
--- /dev/null
+++ b/coccinelle/empty-or-root.cocci
@@ -0,0 +1,10 @@
+@@
+expression s;
+@@
+- (isempty(s) || path_equal(s, "/"))
++ empty_or_root(s)
+@@
+expression s;
+@@
+- (!isempty(s) && !path_equal(s, "/"))
++ !empty_or_root(s)
diff --git a/coccinelle/empty-to-null.cocci b/coccinelle/empty-to-null.cocci
new file mode 100644
index 0000000..fbc75b9
--- /dev/null
+++ b/coccinelle/empty-to-null.cocci
@@ -0,0 +1,5 @@
+@@
+expression s;
+@@
+- isempty(s) ? NULL : s
++ empty_to_null(s)
diff --git a/coccinelle/enotsup.cocci b/coccinelle/enotsup.cocci
new file mode 100644
index 0000000..c65734d
--- /dev/null
+++ b/coccinelle/enotsup.cocci
@@ -0,0 +1,4 @@
+@@
+@@
+- ENOTSUP
++ EOPNOTSUPP
diff --git a/coccinelle/equals-null.cocci b/coccinelle/equals-null.cocci
new file mode 100644
index 0000000..957d828
--- /dev/null
+++ b/coccinelle/equals-null.cocci
@@ -0,0 +1,14 @@
+@@
+expression e;
+statement s;
+@@
+- if (e == NULL)
++ if (!e)
+s
+@@
+expression e;
+statement s;
+@@
+- if (e != NULL)
++ if (e)
+s
diff --git a/coccinelle/errno.cocci b/coccinelle/errno.cocci
new file mode 100644
index 0000000..ed74c0a
--- /dev/null
+++ b/coccinelle/errno.cocci
@@ -0,0 +1,32 @@
+@@
+identifier log_LEVEL_errno =~ "^log_(debug|info|notice|warning|error|emergency)_errno$";
+local idexpression r;
+expression e;
+@@
+- r = -e;
++ r =
+ log_LEVEL_errno(e, ...);
+@@
+identifier log_LEVEL_errno =~ "^log_(debug|info|notice|warning|error|emergency)_errno$";
+local idexpression r;
+expression e;
+@@
++ r =
+ log_LEVEL_errno(e, ...);
+- r = -e;
+@@
+identifier log_LEVEL_errno =~ "^log_(debug|info|notice|warning|error|emergency)_errno$";
+local idexpression r;
+expression e;
+@@
+- r =
++ return
+ log_LEVEL_errno(e, ...);
+- return r;
+@@
+identifier log_LEVEL_errno =~ "^log_(debug|info|notice|warning|error|emergency)_errno$";
+expression e;
+@@
++ return
+ log_LEVEL_errno(e, ...);
+- return -e;
diff --git a/coccinelle/exit-0.cocci b/coccinelle/exit-0.cocci
new file mode 100644
index 0000000..8b81600
--- /dev/null
+++ b/coccinelle/exit-0.cocci
@@ -0,0 +1,16 @@
+@@
+@@
+- exit(0);
++ exit(EXIT_SUCCESS);
+@@
+@@
+- _exit(0);
++ _exit(EXIT_SUCCESS);
+@@
+@@
+- exit(1);
++ exit(EXIT_FAILURE);
+@@
+@@
+- _exit(1);
++ _exit(EXIT_FAILURE);
diff --git a/coccinelle/flags-set.cocci b/coccinelle/flags-set.cocci
new file mode 100644
index 0000000..1a70717
--- /dev/null
+++ b/coccinelle/flags-set.cocci
@@ -0,0 +1,15 @@
+@@
+expression x, y;
+@@
+- ((x) & (y)) == (y)
++ FLAGS_SET(x, y)
+@@
+expression x, y;
+@@
+- (x & (y)) == (y)
++ FLAGS_SET(x, y)
+@@
+expression x, y;
+@@
+- ((x) & y) == y
++ FLAGS_SET(x, y)
diff --git a/coccinelle/free_and_replace.cocci b/coccinelle/free_and_replace.cocci
new file mode 100644
index 0000000..9dcdbf4
--- /dev/null
+++ b/coccinelle/free_and_replace.cocci
@@ -0,0 +1,15 @@
+@@
+expression p, q;
+@@
+- free(p);
+- p = q;
+- q = NULL;
+- return 0;
++ return free_and_replace(p, q);
+@@
+expression p, q;
+@@
+- free(p);
+- p = q;
+- q = NULL;
++ free_and_replace(p, q);
diff --git a/coccinelle/hashmap_free.cocci b/coccinelle/hashmap_free.cocci
new file mode 100644
index 0000000..86b9542
--- /dev/null
+++ b/coccinelle/hashmap_free.cocci
@@ -0,0 +1,54 @@
+@@
+expression p;
+@@
+- set_free(p);
+- p = NULL;
++ p = set_free(p);
+@@
+expression p;
+@@
+- if (p)
+- set_free(p);
+- p = NULL;
++ p = set_free(p);
+@@
+expression p;
+@@
+- if (p) {
+- set_free(p);
+- p = NULL;
+- }
++ p = set_free(p);
+@@
+expression p;
+@@
+- if (p)
+- set_free(p);
++ set_free(p);
+@@
+expression p;
+@@
+- hashmap_free(p);
+- p = NULL;
++ p = hashmap_free(p);
+@@
+expression p;
+@@
+- if (p)
+- hashmap_free(p);
+- p = NULL;
++ p = hashmap_free(p);
+@@
+expression p;
+@@
+- if (p) {
+- hashmap_free(p);
+- p = NULL;
+- }
++ p = hashmap_free(p);
+@@
+expression p;
+@@
+- if (p)
+- hashmap_free(p);
++ hashmap_free(p);
diff --git a/coccinelle/htonl.cocci b/coccinelle/htonl.cocci
new file mode 100644
index 0000000..4e69bb7
--- /dev/null
+++ b/coccinelle/htonl.cocci
@@ -0,0 +1,20 @@
+@@
+expression s;
+@@
+- htonl(s)
++ htobe32(s)
+@@
+expression s;
+@@
+- htons(s)
++ htobe16(s)
+@@
+expression s;
+@@
+- ntohl(s)
++ be32toh(s)
+@@
+expression s;
+@@
+- ntohs(s)
++ be16toh(s)
diff --git a/coccinelle/in_set.cocci b/coccinelle/in_set.cocci
new file mode 100644
index 0000000..12d5475
--- /dev/null
+++ b/coccinelle/in_set.cocci
@@ -0,0 +1,54 @@
+@@
+expression e;
+constant n0, n1, n2, n3, n4, n5, n6, n7, n8, n9;
+@@
+- e == n0 || e == n1 || e == n2 || e == n3 || e == n4 || e == n5 || e == n6 || e == n7 || e == n8 || e == n9
++ IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9)
+@@
+expression e;
+constant n0, n1, n2, n3, n4, n5, n6, n7, n8;
+@@
+- e == n0 || e == n1 || e == n2 || e == n3 || e == n4 || e == n5 || e == n6 || e == n7 || e == n8
++ IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7, n8)
+@@
+expression e;
+constant n0, n1, n2, n3, n4, n5, n6, n7;
+@@
+- e == n0 || e == n1 || e == n2 || e == n3 || e == n4 || e == n5 || e == n6 || e == n7
++ IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7)
+@@
+expression e;
+constant n0, n1, n2, n3, n4, n5, n6;
+@@
+- e == n0 || e == n1 || e == n2 || e == n3 || e == n4 || e == n5 || e == n6
++ IN_SET(e, n0, n1, n2, n3, n4, n5, n6)
+@@
+expression e;
+constant n0, n1, n2, n3, n4, n5;
+@@
+- e == n0 || e == n1 || e == n2 || e == n3 || e == n4 || e == n5
++ IN_SET(e, n0, n1, n2, n3, n4, n5)
+@@
+expression e;
+constant n0, n1, n2, n3, n4;
+@@
+- e == n0 || e == n1 || e == n2 || e == n3 || e == n4
++ IN_SET(e, n0, n1, n2, n3, n4)
+@@
+expression e;
+constant n0, n1, n2, n3;
+@@
+- e == n0 || e == n1 || e == n2 || e == n3
++ IN_SET(e, n0, n1, n2, n3)
+@@
+expression e;
+constant n0, n1, n2;
+@@
+- e == n0 || e == n1 || e == n2
++ IN_SET(e, n0, n1, n2)
+@@
+expression e;
+constant n0, n1;
+@@
+- e == n0 || e == n1
++ IN_SET(e, n0, n1)
diff --git a/coccinelle/iovec-make.cocci b/coccinelle/iovec-make.cocci
new file mode 100644
index 0000000..7a0d4ce
--- /dev/null
+++ b/coccinelle/iovec-make.cocci
@@ -0,0 +1,29 @@
+@@
+expression x, y, p, l;
+@@
+- x[y].iov_base = p;
+- x[y].iov_len = l;
+- y++;
++ x[y++] = IOVEC_MAKE(p, l);
+@@
+expression x, p, l;
+@@
+- x.iov_base = p;
+- x.iov_len = l;
++ x = IOVEC_MAKE(p, l);
+@@
+expression x, p, l;
+@@
+- x->iov_base = p;
+- x->iov_len = l;
++ *x = IOVEC_MAKE(p, l);
+@@
+expression s;
+@@
+- IOVEC_MAKE(s, strlen(s));
++ IOVEC_MAKE_STRING(s);
+@@
+expression x, y, z;
+@@
+- x = (struct iovec) { .iov_base = y, .iov_len = z };
++ x = IOVEC_MAKE(y, z);
diff --git a/coccinelle/isempty.cocci b/coccinelle/isempty.cocci
new file mode 100644
index 0000000..d8d5275
--- /dev/null
+++ b/coccinelle/isempty.cocci
@@ -0,0 +1,60 @@
+@@
+expression s;
+@@
+- strv_length(s) == 0
++ strv_isempty(s)
+@@
+expression s;
+@@
+- strv_length(s) <= 0
++ strv_isempty(s)
+@@
+expression s;
+@@
+- strv_length(s) > 0
++ !strv_isempty(s)
+@@
+expression s;
+@@
+- strv_length(s) != 0
++ !strv_isempty(s)
+@@
+expression s;
+@@
+- strlen(s) == 0
++ isempty(s)
+@@
+expression s;
+@@
+- strlen(s) <= 0
++ isempty(s)
+@@
+expression s;
+@@
+- strlen(s) > 0
++ !isempty(s)
+@@
+expression s;
+@@
+- strlen(s) != 0
++ !isempty(s)
+@@
+expression s;
+@@
+- strlen_ptr(s) == 0
++ isempty(s)
+@@
+expression s;
+@@
+- strlen_ptr(s) <= 0
++ isempty(s)
+@@
+expression s;
+@@
+- strlen_ptr(s) > 0
++ !isempty(s)
+@@
+expression s;
+@@
+- strlen_ptr(s) != 0
++ !isempty(s)
diff --git a/coccinelle/malloc_multiply.cocci b/coccinelle/malloc_multiply.cocci
new file mode 100644
index 0000000..3284edf
--- /dev/null
+++ b/coccinelle/malloc_multiply.cocci
@@ -0,0 +1,20 @@
+@@
+expression q, n, m;
+@@
+- q = malloc((n)*(m))
++ q = malloc_multiply(n, m)
+@@
+expression q, n, m;
+@@
+- q = malloc(n*(m))
++ q = malloc_multiply(n, m)
+@@
+expression q, n, m;
+@@
+- q = malloc((n)*m)
++ q = malloc_multiply(n, m)
+@@
+expression q, n, m;
+@@
+- q = malloc(n*m)
++ q = malloc_multiply(n, m)
diff --git a/coccinelle/memzero.cocci b/coccinelle/memzero.cocci
new file mode 100644
index 0000000..ebdc3f6
--- /dev/null
+++ b/coccinelle/memzero.cocci
@@ -0,0 +1,30 @@
+@@
+expression s;
+@@
+- memset(&s, 0, sizeof(s))
++ zero(s)
+@@
+expression s;
+@@
+- memset(s, 0, sizeof(*s))
++ zero(*s)
+@@
+expression s;
+@@
+- bzero(&s, sizeof(s))
++ zero(s)
+@@
+expression s;
+@@
+- bzero(s, sizeof(*s))
++ zero(*s)
+@@
+expression a, b;
+@@
+- memset(a, 0, b)
++ memzero(a, b)
+@@
+expression a, b;
+@@
+- bzero(a, b)
++ memzero(a, b)
diff --git a/coccinelle/mfree.cocci b/coccinelle/mfree.cocci
new file mode 100644
index 0000000..1389cd3
--- /dev/null
+++ b/coccinelle/mfree.cocci
@@ -0,0 +1,6 @@
+@@
+expression p;
+@@
+- free(p);
+- p = NULL;
++ p = mfree(p);
diff --git a/coccinelle/mfree_return.cocci b/coccinelle/mfree_return.cocci
new file mode 100644
index 0000000..8119fe0
--- /dev/null
+++ b/coccinelle/mfree_return.cocci
@@ -0,0 +1,6 @@
+@@
+expression p;
+@@
+- free(p);
+- return NULL;
++ return mfree(p);
diff --git a/coccinelle/no-if-assignments.cocci b/coccinelle/no-if-assignments.cocci
new file mode 100644
index 0000000..9f63e90
--- /dev/null
+++ b/coccinelle/no-if-assignments.cocci
@@ -0,0 +1,20 @@
+@@
+expression p, q;
+identifier r;
+statement s;
+@@
+- if ((r = q) < p)
+- s
++ r = q;
++ if (r < p)
++ s
+@@
+expression p, q;
+identifier r;
+statement s;
+@@
+- if ((r = q) >= p)
+- s
++ r = q;
++ if (r >= p)
++ s
diff --git a/coccinelle/not_in_set.cocci b/coccinelle/not_in_set.cocci
new file mode 100644
index 0000000..7cf9850
--- /dev/null
+++ b/coccinelle/not_in_set.cocci
@@ -0,0 +1,54 @@
+@@
+expression e;
+constant n0, n1, n2, n3, n4, n5, n6, n7, n8, n9;
+@@
+- e != n0 && e != n1 && e != n2 && e != n3 && e != n4 && e != n5 && e != n6 && e != n7 && e != n8 && e != n9
++ !IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9)
+@@
+expression e;
+constant n0, n1, n2, n3, n4, n5, n6, n7, n8;
+@@
+- e != n0 && e != n1 && e != n2 && e != n3 && e != n4 && e != n5 && e != n6 && e != n7 && e != n8
++ !IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7, n8)
+@@
+expression e;
+constant n0, n1, n2, n3, n4, n5, n6, n7;
+@@
+- e != n0 && e != n1 && e != n2 && e != n3 && e != n4 && e != n5 && e != n6 && e != n7
++ !IN_SET(e, n0, n1, n2, n3, n4, n5, n6, n7)
+@@
+expression e;
+constant n0, n1, n2, n3, n4, n5, n6;
+@@
+- e != n0 && e != n1 && e != n2 && e != n3 && e != n4 && e != n5 && e != n6
++ !IN_SET(e, n0, n1, n2, n3, n4, n5, n6)
+@@
+expression e;
+constant n0, n1, n2, n3, n4, n5;
+@@
+- e != n0 && e != n1 && e != n2 && e != n3 && e != n4 && e != n5
++ !IN_SET(e, n0, n1, n2, n3, n4, n5)
+@@
+expression e;
+constant n0, n1, n2, n3, n4;
+@@
+- e != n0 && e != n1 && e != n2 && e != n3 && e != n4
++ !IN_SET(e, n0, n1, n2, n3, n4)
+@@
+expression e;
+constant n0, n1, n2, n3;
+@@
+- e != n0 && e != n1 && e != n2 && e != n3
++ !IN_SET(e, n0, n1, n2, n3)
+@@
+expression e;
+constant n0, n1, n2;
+@@
+- e != n0 && e != n1 && e != n2
++ !IN_SET(e, n0, n1, n2)
+@@
+expression e;
+constant n0, n1;
+@@
+- e != n0 && e != n1
++ !IN_SET(e, n0, n1)
diff --git a/coccinelle/o-ndelay.cocci b/coccinelle/o-ndelay.cocci
new file mode 100644
index 0000000..669424a
--- /dev/null
+++ b/coccinelle/o-ndelay.cocci
@@ -0,0 +1,4 @@
+@@
+@@
+- O_NDELAY
++ O_NONBLOCK
diff --git a/coccinelle/reallocarray.cocci b/coccinelle/reallocarray.cocci
new file mode 100644
index 0000000..21fe9df
--- /dev/null
+++ b/coccinelle/reallocarray.cocci
@@ -0,0 +1,20 @@
+@@
+expression q, p, n, m;
+@@
+- q = realloc(p, (n)*(m))
++ q = reallocarray(p, n, m)
+@@
+expression q, p, n, m;
+@@
+- q = realloc(p, n*(m))
++ q = reallocarray(p, n, m)
+@@
+expression q, p, n, m;
+@@
+- q = realloc(p, (n)*m)
++ q = reallocarray(p, n, m)
+@@
+expression q, p, n, m;
+@@
+- q = realloc(p, n*m)
++ q = reallocarray(p, n, m)
diff --git a/coccinelle/redundant-if.cocci b/coccinelle/redundant-if.cocci
new file mode 100644
index 0000000..515e36e
--- /dev/null
+++ b/coccinelle/redundant-if.cocci
@@ -0,0 +1,54 @@
+@@
+expression r;
+@@
+- if (r < 0)
+- return r;
+- if (r == 0)
+- return 0;
++ if (r <= 0)
++ return r;
+@@
+expression r;
+@@
+- if (r == 0)
+- return 0;
+- if (r < 0)
+- return r;
++ if (r <= 0)
++ return r;
+@@
+expression r;
+@@
+- if (r < 0)
+- return r;
+- if (r == 0)
+- return r;
++ if (r <= 0)
++ return r;
+@@
+expression r;
+@@
+- if (r == 0)
+- return r;
+- if (r < 0)
+- return r;
++ if (r <= 0)
++ return r;
+@@
+expression r;
+@@
+- if (r < 0)
+- return r;
+- if (r > 0)
+- return r;
++ if (r != 0)
++ return r;
+@@
+expression r;
+@@
+- if (r > 0)
+- return r;
+- if (r < 0)
+- return r;
++ if (r != 0)
++ return r;
diff --git a/coccinelle/run-coccinelle.sh b/coccinelle/run-coccinelle.sh
new file mode 100755
index 0000000..22ab66d
--- /dev/null
+++ b/coccinelle/run-coccinelle.sh
@@ -0,0 +1,27 @@
+#!/bin/bash -e
+
+top="$(git rev-parse --show-toplevel)"
+files="$(git ls-files ':/*.[ch]')"
+args=
+
+case "$1" in
+ -i)
+ args="$args --in-place"
+ shift
+ ;;
+esac
+
+if ! parallel -h >/dev/null; then
+ echo 'Please install GNU parallel (package "parallel")'
+ exit 1
+fi
+
+for SCRIPT in ${@-$top/coccinelle/*.cocci} ; do
+ echo "--x-- Processing $SCRIPT --x--"
+ TMPFILE=`mktemp`
+ echo "+ spatch --sp-file $SCRIPT $args ..."
+ parallel --halt now,fail=1 --keep-order --noswap --max-args=20 \
+ spatch --sp-file $SCRIPT $args ::: $files \
+ 2>"$TMPFILE" || cat "$TMPFILE"
+ echo -e "--x-- Processed $SCRIPT --x--\n"
+done
diff --git a/coccinelle/safe_close-no-if.cocci b/coccinelle/safe_close-no-if.cocci
new file mode 100644
index 0000000..81c5678
--- /dev/null
+++ b/coccinelle/safe_close-no-if.cocci
@@ -0,0 +1,7 @@
+@@
+expression fd;
+@@
+- if (fd >= 0) {
+- fd = safe_close(fd);
+- }
++ fd = safe_close(fd);
diff --git a/coccinelle/safe_close.cocci b/coccinelle/safe_close.cocci
new file mode 100644
index 0000000..6fedd80
--- /dev/null
+++ b/coccinelle/safe_close.cocci
@@ -0,0 +1,18 @@
+@@
+expression fd;
+@@
+- close(fd);
+- fd = -1;
++ fd = safe_close(fd);
+@@
+expression fd;
+@@
+- close_nointr(fd);
+- fd = -1;
++ fd = safe_close(fd);
+@@
+expression fd;
+@@
+- safe_close(fd);
+- fd = -1;
++ fd = safe_close(fd);
diff --git a/coccinelle/safe_closedir.cocci b/coccinelle/safe_closedir.cocci
new file mode 100644
index 0000000..743ffd9
--- /dev/null
+++ b/coccinelle/safe_closedir.cocci
@@ -0,0 +1,27 @@
+@@
+expression p;
+@@
+- if (p) {
+- closedir(p);
+- p = NULL;
+- }
++ p = safe_closedir(p);
+@@
+expression p;
+@@
+- if (p)
+- closedir(p);
+- p = NULL;
++ p = safe_closedir(p);
+@@
+expression p;
+@@
+- closedir(p);
+- p = NULL;
++ p = safe_closedir(p);
+@@
+expression p;
+@@
+- if (p)
+- closedir(p);
++ safe_closedir(p);
diff --git a/coccinelle/safe_fclose.cocci b/coccinelle/safe_fclose.cocci
new file mode 100644
index 0000000..6961cd0
--- /dev/null
+++ b/coccinelle/safe_fclose.cocci
@@ -0,0 +1,27 @@
+@@
+expression p;
+@@
+- if (p) {
+- fclose(p);
+- p = NULL;
+- }
++ p = safe_fclose(p);
+@@
+expression p;
+@@
+- if (p)
+- fclose(p);
+- p = NULL;
++ p = safe_fclose(p);
+@@
+expression p;
+@@
+- fclose(p);
+- p = NULL;
++ p = safe_fclose(p);
+@@
+expression p;
+@@
+- if (p)
+- fclose(p);
++ safe_fclose(p);
diff --git a/coccinelle/strempty.cocci b/coccinelle/strempty.cocci
new file mode 100644
index 0000000..13ceb33
--- /dev/null
+++ b/coccinelle/strempty.cocci
@@ -0,0 +1,48 @@
+@@
+expression s;
+@@
+- s ?: ""
++ strempty(s)
+@@
+expression s;
+@@
+- s ? s : ""
++ strempty(s)
+@@
+expression s;
+@@
+- if (!s)
+- s = "";
++ s = strempty(s);
+@@
+expression s;
+@@
+- s ?: "(null)"
++ strnull(s)
+@@
+expression s;
+@@
+- s ? s : "(null)"
++ strnull(s)
+@@
+expression s;
+@@
+- if (!s)
+- s = "(null)";
++ s = strnull(s);
+@@
+expression s;
+@@
+- s ?: "n/a"
++ strna(s)
+@@
+expression s;
+@@
+- s ? s : "n/a"
++ strna(s)
+@@
+expression s;
+@@
+- if (!s)
+- s = "n/a";
++ s = strna(s);
diff --git a/coccinelle/strjoin.cocci b/coccinelle/strjoin.cocci
new file mode 100644
index 0000000..675760e
--- /dev/null
+++ b/coccinelle/strjoin.cocci
@@ -0,0 +1,16 @@
+@@
+expression list args;
+@@
+- strjoin(args, NULL);
++ strjoin(args);
+@@
+expression t;
+expression list args;
+@@
+- t = strjoin(args, NULL);
++ t = strjoin(args);
+@@
+expression list args;
+@@
+- return strjoin(args, NULL);
++ return strjoin(args);
diff --git a/coccinelle/strjoina.cocci b/coccinelle/strjoina.cocci
new file mode 100644
index 0000000..a6236eb
--- /dev/null
+++ b/coccinelle/strjoina.cocci
@@ -0,0 +1,6 @@
+@@
+expression n, m;
+expression list s;
+@@
+- n = strjoina(m, s, NULL);
++ n = strjoina(m, s);
diff --git a/coccinelle/strv_free.cocci b/coccinelle/strv_free.cocci
new file mode 100644
index 0000000..0ad56f7
--- /dev/null
+++ b/coccinelle/strv_free.cocci
@@ -0,0 +1,27 @@
+@@
+expression p;
+@@
+- strv_free(p);
+- p = NULL;
++ p = strv_free(p);
+@@
+expression p;
+@@
+- if (p)
+- strv_free(p);
+- p = NULL;
++ p = strv_free(p);
+@@
+expression p;
+@@
+- if (p) {
+- strv_free(p);
+- p = NULL;
+- }
++ p = strv_free(p);
+@@
+expression p;
+@@
+- if (p)
+- strv_free(p);
++ strv_free(p);
diff --git a/coccinelle/swap-two.cocci b/coccinelle/swap-two.cocci
new file mode 100644
index 0000000..edf7d32
--- /dev/null
+++ b/coccinelle/swap-two.cocci
@@ -0,0 +1,7 @@
+@@
+expression x, y, z;
+@@
+- z = x;
+- x = y;
+- y = z;
++ SWAP_TWO(x, y);
diff --git a/coccinelle/synthetic-errno.cocci b/coccinelle/synthetic-errno.cocci
new file mode 100644
index 0000000..645bfc9
--- /dev/null
+++ b/coccinelle/synthetic-errno.cocci
@@ -0,0 +1,42 @@
+@@
+expression e;
+expression list args;
+@@
+- log_debug(args);
+- return -e;
++ return log_debug_errno(SYNTHETIC_ERRNO(e), args);
+@@
+expression e;
+expression list args;
+@@
+- log_info(args);
+- return -e;
++ return log_info_errno(SYNTHETIC_ERRNO(e), args);
+@@
+expression e;
+expression list args;
+@@
+- log_notice(args);
+- return -e;
++ return log_notice_errno(SYNTHETIC_ERRNO(e), args);
+@@
+expression e;
+expression list args;
+@@
+- log_error(args);
+- return -e;
++ return log_error_errno(SYNTHETIC_ERRNO(e), args);
+@@
+expression e;
+expression list args;
+@@
+- log_emergency(args);
+- return -e;
++ return log_emergency_errno(SYNTHETIC_ERRNO(e), args);
+@@
+identifier log_LEVEL_errno =~ "^log_(debug|info|notice|warning|error|emergency)_errno$";
+identifier ERRNO =~ "^E[A-Z]+$";
+expression list args;
+@@
+- return log_LEVEL_errno(ERRNO, args);
++ return log_LEVEL_errno(SYNTHETIC_ERRNO(ERRNO), args);
diff --git a/coccinelle/take-fd.cocci b/coccinelle/take-fd.cocci
new file mode 100644
index 0000000..ba24248
--- /dev/null
+++ b/coccinelle/take-fd.cocci
@@ -0,0 +1,14 @@
+@@
+local idexpression p;
+expression q;
+@@
+- p = q;
+- q = -1;
+- return p;
++ return TAKE_FD(q);
+@@
+expression p, q;
+@@
+- p = q;
+- q = -1;
++ p = TAKE_FD(q);
diff --git a/coccinelle/take-ptr.cocci b/coccinelle/take-ptr.cocci
new file mode 100644
index 0000000..0cebe81
--- /dev/null
+++ b/coccinelle/take-ptr.cocci
@@ -0,0 +1,14 @@
+@@
+local idexpression p;
+expression q;
+@@
+- p = q;
+- q = NULL;
+- return p;
++ return TAKE_PTR(q);
+@@
+expression p, q;
+@@
+- p = q;
+- q = NULL;
++ p = TAKE_PTR(q);
diff --git a/coccinelle/while-true.cocci b/coccinelle/while-true.cocci
new file mode 100644
index 0000000..c23fb11
--- /dev/null
+++ b/coccinelle/while-true.cocci
@@ -0,0 +1,12 @@
+@@
+statement s;
+@@
+- while (true)
++ for (;;)
+s
+@@
+statement s;
+@@
+- while (1)
++ for (;;)
+s
diff --git a/coccinelle/xsprintf.cocci b/coccinelle/xsprintf.cocci
new file mode 100644
index 0000000..401216a
--- /dev/null
+++ b/coccinelle/xsprintf.cocci
@@ -0,0 +1,6 @@
+@@
+expression e, fmt;
+expression list vaargs;
+@@
+- snprintf(e, sizeof(e), fmt, vaargs);
++ xsprintf(e, fmt, vaargs);