From 96ca229517c65c7f47c02e21e9778ac189a829c1 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 16 Feb 2022 10:52:51 +0100 Subject: coccinelle: automatically switch some uses of memcpy() → mempcpy() 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)); -- cgit v1.2.3