diff options
author | Mike Yuan <me@yhndnzj.com> | 2024-09-19 23:08:42 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2024-09-20 01:29:35 +0200 |
commit | eda6223942a172fa6777901cf5fbd47438f285ce (patch) | |
tree | f0277cf8788d4141b7652ca8bc5b6110a49a0d68 | |
parent | boot: use INC_SAFE where appropriate (diff) | |
download | systemd-eda6223942a172fa6777901cf5fbd47438f285ce.tar.xz systemd-eda6223942a172fa6777901cf5fbd47438f285ce.zip |
basic/memory-util: introduce mempcpy_typesafe
-rw-r--r-- | src/basic/memory-util.h | 6 | ||||
-rw-r--r-- | src/coredump/coredump.c | 2 | ||||
-rw-r--r-- | src/journal/journald-audit.c | 2 | ||||
-rw-r--r-- | src/journal/journald-server.c | 14 | ||||
-rw-r--r-- | src/sysupdate/sysupdate-cache.c | 2 | ||||
-rw-r--r-- | src/timedate/timedated.c | 2 | ||||
-rw-r--r-- | src/tmpfiles/tmpfiles.c | 2 |
7 files changed, 16 insertions, 14 deletions
diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 294aed67df..2e10d33bb3 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -20,7 +20,7 @@ size_t page_size(void) _pure_; #define PAGE_OFFSET_U64(l) ALIGN_OFFSET_U64(l, page_size()) /* Normal memcpy() requires src to be nonnull. We do nothing if n is 0. */ -static inline void *memcpy_safe(void *dst, const void *src, size_t n) { +static inline void* memcpy_safe(void *dst, const void *src, size_t n) { if (n == 0) return dst; assert(src); @@ -28,13 +28,15 @@ static inline void *memcpy_safe(void *dst, const void *src, size_t n) { } /* Normal mempcpy() requires src to be nonnull. We do nothing if n is 0. */ -static inline void *mempcpy_safe(void *dst, const void *src, size_t n) { +static inline void* mempcpy_safe(void *dst, const void *src, size_t n) { if (n == 0) return dst; assert(src); return mempcpy(dst, src, n); } +#define mempcpy_typesafe(dst, src, n) (typeof((dst)[0])*) mempcpy_safe(dst, src, n) + /* Normal memcmp() requires s1 and s2 to be nonnull. We do nothing if n is 0. */ static inline int memcmp_safe(const void *s1, const void *s2, size_t n) { if (n == 0) diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index fdf30a33f1..4a01fea5f9 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -1366,7 +1366,7 @@ static int gather_pid_metadata_from_procfs(struct iovec_wrapper *iovw, Context * char *buf = malloc(strlen("COREDUMP_PROC_AUXV=") + size + 1); if (buf) { /* Add a dummy terminator to make save_context() happy. */ - *((uint8_t*) mempcpy(stpcpy(buf, "COREDUMP_PROC_AUXV="), t, size)) = '\0'; + *mempcpy_typesafe(stpcpy(buf, "COREDUMP_PROC_AUXV="), t, size) = '\0'; (void) iovw_consume(iovw, buf, size + strlen("COREDUMP_PROC_AUXV=")); } diff --git a/src/journal/journald-audit.c b/src/journal/journald-audit.c index d49283d225..55f8ce7140 100644 --- a/src/journal/journald-audit.c +++ b/src/journal/journald-audit.c @@ -87,7 +87,7 @@ static int map_string_field_internal( if (!c) return -ENOMEM; - *((char*) mempcpy(stpcpy(c, field), s, e - s)) = 0; + *mempcpy_typesafe(stpcpy(c, field), s, e - s) = 0; e += 1; diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index 859e5366d0..9377ff8c79 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -1066,13 +1066,13 @@ static void server_write_to_journal( iovec[n++] = IOVEC_MAKE_STRING(k); \ } -#define IOVEC_ADD_SIZED_FIELD(iovec, n, value, value_size, field) \ - if (value_size > 0) { \ - char *k; \ - k = newa(char, STRLEN(field "=") + value_size + 1); \ - *((char*) mempcpy(stpcpy(k, field "="), value, value_size)) = 0; \ - iovec[n++] = IOVEC_MAKE_STRING(k); \ - } \ +#define IOVEC_ADD_SIZED_FIELD(iovec, n, value, value_size, field) \ + if (value_size > 0) { \ + char *k; \ + k = newa(char, STRLEN(field "=") + value_size + 1); \ + *mempcpy_typesafe(stpcpy(k, field "="), value, value_size) = 0; \ + iovec[n++] = IOVEC_MAKE_STRING(k); \ + } static void server_dispatch_message_real( Server *s, diff --git a/src/sysupdate/sysupdate-cache.c b/src/sysupdate/sysupdate-cache.c index 8dad3ee479..bc3a62bbb2 100644 --- a/src/sysupdate/sysupdate-cache.c +++ b/src/sysupdate/sysupdate-cache.c @@ -62,7 +62,7 @@ int web_cache_add_item( }; /* Just to be extra paranoid, let's NUL terminate the downloaded buffer */ - *(uint8_t*) mempcpy(item->data, data, size) = 0; + *mempcpy_typesafe(item->data, data, size) = 0; web_cache_item_free(hashmap_remove(*web_cache, url)); diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index 58e8a0a0df..ea99a77c85 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -380,7 +380,7 @@ static int context_write_data_local_rtc(Context *c) { if (!w) return -ENOMEM; - *(char*) mempcpy(stpcpy(stpcpy(mempcpy(w, s, a), prepend), c->local_rtc ? "LOCAL" : "UTC"), e, b) = 0; + *mempcpy_typesafe(stpcpy(stpcpy(mempcpy(w, s, a), prepend), c->local_rtc ? "LOCAL" : "UTC"), e, b) = 0; if (streq(w, NULL_ADJTIME_UTC)) { if (unlink("/etc/adjtime") < 0) diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 16f831bc26..12fb5f50f0 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -2756,7 +2756,7 @@ static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) { e = s + strcspn(s, "/"); /* Copy the path component to t so it can be a null terminated string. */ - *((char*) mempcpy(t, s, e - s)) = 0; + *mempcpy_typesafe(t, s, e - s) = 0; /* Is this the last component? If so, then check the type */ if (*e == 0) |