diff --git a/src/basic/alloc-util.c b/src/basic/alloc-util.c index 5545af5497..b030f454b2 100644 --- a/src/basic/alloc-util.c +++ b/src/basic/alloc-util.c @@ -17,8 +17,7 @@ void* memdup(const void *p, size_t l) { if (!ret) return NULL; - memcpy(ret, p, l); - return ret; + return memcpy_safe(ret, p, l); } void* memdup_suffix0(const void *p, size_t l) { @@ -35,8 +34,8 @@ void* memdup_suffix0(const void *p, size_t l) { if (!ret) return NULL; - *((uint8_t*) mempcpy(ret, p, l)) = 0; - return ret; + ((uint8_t*) ret)[l] = 0; + return memcpy_safe(ret, p, l); } void* greedy_realloc( diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index 3ef126955b..99fbd3a889 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -66,7 +66,7 @@ void* memdup_suffix0(const void *p, size_t l); /* We can't use _alloc_() here, s size_t _l_ = l; \ assert(_l_ <= ALLOCA_MAX); \ _q_ = alloca(_l_ ?: 1); \ - memcpy(_q_, p, _l_); \ + memcpy_safe(_q_, p, _l_); \ }) #define memdupa_suffix0(p, l) \ @@ -76,7 +76,7 @@ void* memdup_suffix0(const void *p, size_t l); /* We can't use _alloc_() here, s assert(_l_ <= ALLOCA_MAX); \ _q_ = alloca(_l_ + 1); \ ((uint8_t*) _q_)[_l_] = 0; \ - memcpy(_q_, p, _l_); \ + memcpy_safe(_q_, p, _l_); \ }) static inline void freep(void *p) { diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index e3f7980d12..0b04278ab4 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -16,11 +16,11 @@ size_t page_size(void) _pure_; #define PAGE_OFFSET(l) ((l) & (page_size() - 1)) /* 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; + return dst; assert(src); - memcpy(dst, src, n); + return memcpy(dst, src, n); } /* Normal memcmp requires s1 and s2 to be nonnull. We do nothing if n is 0. */