]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/memory-util: make mempcpy_typesafe() take number of obj rather than raw size
authorMike Yuan <me@yhndnzj.com>
Fri, 20 Sep 2024 19:25:48 +0000 (21:25 +0200)
committerMike Yuan <me@yhndnzj.com>
Fri, 20 Sep 2024 20:44:34 +0000 (22:44 +0200)
Follow-up for eda6223942a172fa6777901cf5fbd47438f285ce

src/basic/memory-util.h

index 2e10d33bb3f0b11efce4b08585f4832965863114..1f604cc4525a1aa04b2d3b4254b977d84a91aaeb 100644 (file)
@@ -35,7 +35,12 @@ static inline void* mempcpy_safe(void *dst, const void *src, size_t n) {
         return mempcpy(dst, src, n);
 }
 
-#define mempcpy_typesafe(dst, src, n) (typeof((dst)[0])*) mempcpy_safe(dst, src, n)
+#define mempcpy_typesafe(dst, src, n)                                   \
+        ({                                                              \
+                size_t _sz_;                                            \
+                assert_se(MUL_SAFE(&_sz_, sizeof((dst)[0]), n));        \
+                (typeof((dst)[0])*) mempcpy_safe(dst, src, _sz_);       \
+        })
 
 /* 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) {