From: Mike Yuan Date: Thu, 19 Sep 2024 21:08:42 +0000 (+0200) Subject: basic/memory-util: introduce mempcpy_typesafe X-Git-Tag: v257-rc1~389 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=eda6223942a172fa6777901cf5fbd47438f285ce;p=thirdparty%2Fsystemd.git basic/memory-util: introduce mempcpy_typesafe --- diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 294aed67dff..2e10d33bb3f 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 fdf30a33f1a..4a01fea5f95 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 d49283d225c..55f8ce71402 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 859e5366d0d..9377ff8c795 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 8dad3ee479f..bc3a62bbb21 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 58e8a0a0dfc..ea99a77c85f 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 16f831bc262..12fb5f50f03 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)