]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
memory-util: make mempcpy_typesafe nestable
authorMike Yuan <me@yhndnzj.com>
Thu, 20 Mar 2025 16:27:39 +0000 (17:27 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Sat, 22 Mar 2025 15:51:44 +0000 (15:51 +0000)
(cherry picked from commit b062cf19f4a185afd2d86ccf8a85d77bba6f93e6)

src/basic/memory-util.h

index 1f604cc4525a1aa04b2d3b4254b977d84a91aaeb..b1ab4a2a0f5cb833cc2d647352ce9487d8db3d06 100644 (file)
@@ -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)