Merge pull request #19662 from yuwata/memdup

util: make memdup() or friends safer
This commit is contained in:
Lennart Poettering 2021-05-19 23:24:55 +02:00 committed by GitHub
commit 2adcf6f4f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 9 deletions

View file

@ -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(

View file

@ -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) {

View file

@ -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. */