From 96ca229517c65c7f47c02e21e9778ac189a829c1 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 16 Feb 2022 10:52:51 +0100 Subject: [PATCH 1/2] =?UTF-8?q?coccinelle:=20automatically=20switch=20some?= =?UTF-8?q?=20uses=20of=20memcpy()=20=E2=86=92=20mempcpy()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inspired by #22520, let's add a coccinelle script that converts this automatically. --- coccinelle/mempcpy.cocci | 13 +++++++++++++ src/basic/hexdecoct.c | 3 +-- src/libsystemd/sd-journal/catalog.c | 11 ++++------- 3 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 coccinelle/mempcpy.cocci diff --git a/coccinelle/mempcpy.cocci b/coccinelle/mempcpy.cocci new file mode 100644 index 0000000000..efb657ae79 --- /dev/null +++ b/coccinelle/mempcpy.cocci @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +@@ +expression x, y, z; +@@ +- memcpy(x, y, z); +- x += z; ++ x = mempcpy(x, y, z); +@@ +expression x, y, z; +@@ +- memcpy_safe(x, y, z); +- x += z; ++ x = mempcpy_safe(x, y, z); diff --git a/src/basic/hexdecoct.c b/src/basic/hexdecoct.c index 8c83a0e71a..190fca8ee4 100644 --- a/src/basic/hexdecoct.c +++ b/src/basic/hexdecoct.c @@ -683,8 +683,7 @@ static int base64_append_width( s += indent; } - memcpy(s, x + width * line, act); - s += act; + s = mempcpy(s, x + width * line, act); *(s++) = line < lines - 1 ? '\n' : '\0'; avail -= act; } diff --git a/src/libsystemd/sd-journal/catalog.c b/src/libsystemd/sd-journal/catalog.c index 4a2ba02ad0..16bd4a63f1 100644 --- a/src/libsystemd/sd-journal/catalog.c +++ b/src/libsystemd/sd-journal/catalog.c @@ -125,15 +125,12 @@ static char *combine_entries(const char *one, const char *two) { /* Body from @one */ n = l1 - (b1 - one); - if (n > 0) { - memcpy(p, b1, n); - p += n; - + if (n > 0) + p = mempcpy(p, b1, n); /* Body from @two */ - } else { + else { n = l2 - (b2 - two); - memcpy(p, b2, n); - p += n; + p = mempcpy(p, b2, n); } assert(p - dest <= (ptrdiff_t)(l1 + l2)); From 803e12f36b9e5b22491587136e8671e67a41bf2e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 16 Feb 2022 11:50:48 +0100 Subject: [PATCH 2/2] tree-wide: some coccinelle fixes --- src/network/tc/tc.c | 3 +-- src/resolve/test-resolved-stream.c | 10 ++++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/network/tc/tc.c b/src/network/tc/tc.c index 7be2de6354..796036c071 100644 --- a/src/network/tc/tc.c +++ b/src/network/tc/tc.c @@ -183,9 +183,8 @@ int request_process_traffic_control(Request *req) { tc = ASSERT_PTR(req->traffic_control); r = traffic_control_is_ready_to_configure(link, tc); - if (r <= 0) { + if (r <= 0) return r; - } r = traffic_control_configure(link, tc); if (r < 0) diff --git a/src/resolve/test-resolved-stream.c b/src/resolve/test-resolved-stream.c index beaa855384..2f6245f406 100644 --- a/src/resolve/test-resolved-stream.c +++ b/src/resolve/test-resolved-stream.c @@ -221,13 +221,11 @@ static void test_dns_stream(bool tls) { log_info("test-resolved-stream: Started %s test", tls ? "TLS" : "TCP"); #if ENABLE_DNS_OVER_TLS - if (tls) { - /* For TLS mode, use DNS_OVER_TLS_OPPORTUNISTIC instead of - * DNS_OVER_TLS_YES, just to make certificate validation more - * lenient, allowing us to use self-signed certificates. - * We never downgrade, everything we test always goes over TLS */ + if (tls) + /* For TLS mode, use DNS_OVER_TLS_OPPORTUNISTIC instead of DNS_OVER_TLS_YES, just to make + * certificate validation more lenient, allowing us to use self-signed certificates. We + * never downgrade, everything we test always goes over TLS */ manager.dns_over_tls_mode = DNS_OVER_TLS_OPPORTUNISTIC; - } #endif assert_se(sd_event_new(&event) >= 0);