Skip to content

Commit

Permalink
coccinelle: automatically switch some uses of memcpy() → mempcpy()
Browse files Browse the repository at this point in the history
Inspired by systemd#22520, let's add a coccinelle script that converts this
automatically.
  • Loading branch information
poettering committed Feb 16, 2022
1 parent bde335f commit 96ca229
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
13 changes: 13 additions & 0 deletions coccinelle/mempcpy.cocci
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 1 addition & 2 deletions src/basic/hexdecoct.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 4 additions & 7 deletions src/libsystemd/sd-journal/catalog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 96ca229

Please sign in to comment.