mirror of
https://github.com/systemd/systemd
synced 2024-07-21 10:17:21 +00:00
Merge pull request #19662 from yuwata/memdup
util: make memdup() or friends safer
This commit is contained in:
commit
2adcf6f4f7
|
@ -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(
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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. */
|
||||
|
|
Loading…
Reference in a new issue