1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

basic/memory-util: introduce mempcpy_typesafe

This commit is contained in:
Mike Yuan 2024-09-19 23:08:42 +02:00 committed by Yu Watanabe
parent 48837c6500
commit eda6223942
7 changed files with 16 additions and 14 deletions

View File

@ -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)

View File

@ -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="));
}

View File

@ -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;

View File

@ -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,

View File

@ -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));

View File

@ -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)

View File

@ -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)