From: Mike Yuan Date: Thu, 20 Mar 2025 16:27:39 +0000 (+0100) Subject: memory-util: make mempcpy_typesafe nestable X-Git-Tag: v257.5~99 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6c07172d72e8ffbaf9f99e18d0d8146188faa73f;p=thirdparty%2Fsystemd.git memory-util: make mempcpy_typesafe nestable (cherry picked from commit b062cf19f4a185afd2d86ccf8a85d77bba6f93e6) --- diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 1f604cc4525..b1ab4a2a0f5 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -35,13 +35,16 @@ static inline void* mempcpy_safe(void *dst, const void *src, size_t n) { return mempcpy(dst, src, n); } -#define mempcpy_typesafe(dst, src, n) \ +#define _mempcpy_typesafe(dst, src, n, sz) \ ({ \ - size_t _sz_; \ - assert_se(MUL_SAFE(&_sz_, sizeof((dst)[0]), n)); \ - (typeof((dst)[0])*) mempcpy_safe(dst, src, _sz_); \ + size_t sz; \ + assert_se(MUL_SAFE(&sz, sizeof((dst)[0]), n)); \ + (typeof((dst)[0])*) mempcpy_safe(dst, src, sz); \ }) +#define mempcpy_typesafe(dst, src, n) \ + _mempcpy_typesafe(dst, src, n, UNIQ_T(sz, UNIQ)) + /* 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)