From 47941afd1757ed1cfd50faa364fd9624e3a6e892 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Fri, 20 Sep 2024 21:25:48 +0200 Subject: basic/memory-util: make mempcpy_typesafe() take number of obj rather than raw size Follow-up for eda6223942a172fa6777901cf5fbd47438f285ce --- src/basic/memory-util.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 2e10d33bb3..1f604cc452 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -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) { -- cgit v1.2.3