
This is a direct dump from systemd git. ====== SYSTEMD_DIR=../systemd COMMIT=119063d2b149667a91e0e08e4bdf82a0eb6a7efd ( cd "$SYSTEMD_DIR" git checkout "$COMMIT" git reset --hard git clean -fdx ) git ls-files -z :/src/core/systemd/src/ \ :/src/libnm-systemd-shared/src/ \ :/src/libnm-std-aux/unaligned.h | \ xargs -0 rm -f nm_copy_sd_shared() { mkdir -p "./src/libnm-systemd-shared/$(dirname "$1")" cp "$SYSTEMD_DIR/$1" "./src/libnm-systemd-shared/$1" } nm_copy_sd_core() { mkdir -p "./src/core/systemd/$(dirname "$1")" cp "$SYSTEMD_DIR/$1" "./src/core/systemd/$1" } nm_copy_sd_stdaux() { mkdir -p "./src/libnm-std-aux/" cp "$SYSTEMD_DIR/$1" "./src/libnm-std-aux/${1##*/}" } nm_copy_sd_core "src/libsystemd-network/arp-util.c" nm_copy_sd_core "src/libsystemd-network/arp-util.h" nm_copy_sd_core "src/libsystemd-network/dhcp-identifier.c" nm_copy_sd_core "src/libsystemd-network/dhcp-identifier.h" nm_copy_sd_core "src/libsystemd-network/dhcp-internal.h" nm_copy_sd_core "src/libsystemd-network/dhcp-lease-internal.h" nm_copy_sd_core "src/libsystemd-network/dhcp-network.c" nm_copy_sd_core "src/libsystemd-network/dhcp-option.c" nm_copy_sd_core "src/libsystemd-network/dhcp-packet.c" nm_copy_sd_core "src/libsystemd-network/dhcp-protocol.h" nm_copy_sd_core "src/libsystemd-network/dhcp6-internal.h" nm_copy_sd_core "src/libsystemd-network/dhcp6-lease-internal.h" nm_copy_sd_core "src/libsystemd-network/dhcp6-network.c" nm_copy_sd_core "src/libsystemd-network/dhcp6-option.c" nm_copy_sd_core "src/libsystemd-network/dhcp6-protocol.h" nm_copy_sd_core "src/libsystemd-network/lldp-internal.h" nm_copy_sd_core "src/libsystemd-network/lldp-neighbor.c" nm_copy_sd_core "src/libsystemd-network/lldp-neighbor.h" nm_copy_sd_core "src/libsystemd-network/lldp-network.c" nm_copy_sd_core "src/libsystemd-network/lldp-network.h" nm_copy_sd_core "src/libsystemd-network/network-internal.c" nm_copy_sd_core "src/libsystemd-network/network-internal.h" nm_copy_sd_core "src/libsystemd-network/sd-dhcp-client.c" nm_copy_sd_core "src/libsystemd-network/sd-dhcp-lease.c" nm_copy_sd_core "src/libsystemd-network/sd-dhcp6-client.c" nm_copy_sd_core "src/libsystemd-network/sd-dhcp6-lease.c" nm_copy_sd_core "src/libsystemd-network/sd-ipv4acd.c" nm_copy_sd_core "src/libsystemd-network/sd-ipv4ll.c" nm_copy_sd_core "src/libsystemd-network/sd-lldp.c" nm_copy_sd_core "src/libsystemd/sd-event/event-source.h" nm_copy_sd_core "src/libsystemd/sd-event/event-util.c" nm_copy_sd_core "src/libsystemd/sd-event/event-util.h" nm_copy_sd_core "src/libsystemd/sd-event/sd-event.c" nm_copy_sd_core "src/libsystemd/sd-id128/id128-util.c" nm_copy_sd_core "src/libsystemd/sd-id128/id128-util.h" nm_copy_sd_core "src/libsystemd/sd-id128/sd-id128.c" nm_copy_sd_core "src/systemd/_sd-common.h" nm_copy_sd_core "src/systemd/sd-dhcp-client.h" nm_copy_sd_core "src/systemd/sd-dhcp-lease.h" nm_copy_sd_core "src/systemd/sd-dhcp-option.h" nm_copy_sd_core "src/systemd/sd-dhcp6-client.h" nm_copy_sd_core "src/systemd/sd-dhcp6-lease.h" nm_copy_sd_core "src/systemd/sd-dhcp6-option.h" nm_copy_sd_core "src/systemd/sd-event.h" nm_copy_sd_core "src/systemd/sd-id128.h" nm_copy_sd_core "src/systemd/sd-ipv4acd.h" nm_copy_sd_core "src/systemd/sd-ipv4ll.h" nm_copy_sd_core "src/systemd/sd-lldp.h" nm_copy_sd_core "src/systemd/sd-ndisc.h" nm_copy_sd_shared "src/basic/alloc-util.c" nm_copy_sd_shared "src/basic/alloc-util.h" nm_copy_sd_shared "src/basic/async.h" nm_copy_sd_shared "src/basic/cgroup-util.h" nm_copy_sd_shared "src/basic/dns-def.h" nm_copy_sd_shared "src/basic/env-file.c" nm_copy_sd_shared "src/basic/env-file.h" nm_copy_sd_shared "src/basic/env-util.c" nm_copy_sd_shared "src/basic/env-util.h" nm_copy_sd_shared "src/basic/errno-util.h" nm_copy_sd_shared "src/basic/escape.c" nm_copy_sd_shared "src/basic/escape.h" nm_copy_sd_shared "src/basic/ether-addr-util.c" nm_copy_sd_shared "src/basic/ether-addr-util.h" nm_copy_sd_shared "src/basic/extract-word.c" nm_copy_sd_shared "src/basic/extract-word.h" nm_copy_sd_shared "src/basic/fd-util.c" nm_copy_sd_shared "src/basic/fd-util.h" nm_copy_sd_shared "src/basic/fileio.c" nm_copy_sd_shared "src/basic/fileio.h" nm_copy_sd_shared "src/basic/format-util.c" nm_copy_sd_shared "src/basic/format-util.h" nm_copy_sd_shared "src/basic/fs-util.c" nm_copy_sd_shared "src/basic/fs-util.h" nm_copy_sd_shared "src/basic/hash-funcs.c" nm_copy_sd_shared "src/basic/hash-funcs.h" nm_copy_sd_shared "src/basic/hashmap.c" nm_copy_sd_shared "src/basic/hashmap.h" nm_copy_sd_shared "src/basic/hexdecoct.c" nm_copy_sd_shared "src/basic/hexdecoct.h" nm_copy_sd_shared "src/basic/hostname-util.c" nm_copy_sd_shared "src/basic/hostname-util.h" nm_copy_sd_shared "src/basic/in-addr-util.c" nm_copy_sd_shared "src/basic/in-addr-util.h" nm_copy_sd_shared "src/basic/io-util.c" nm_copy_sd_shared "src/basic/io-util.h" nm_copy_sd_shared "src/basic/list.h" nm_copy_sd_shared "src/basic/log.h" nm_copy_sd_shared "src/basic/macro.h" nm_copy_sd_shared "src/basic/memory-util.c" nm_copy_sd_shared "src/basic/memory-util.h" nm_copy_sd_shared "src/basic/mempool.c" nm_copy_sd_shared "src/basic/mempool.h" nm_copy_sd_shared "src/basic/missing_fcntl.h" nm_copy_sd_shared "src/basic/missing_random.h" nm_copy_sd_shared "src/basic/missing_socket.h" nm_copy_sd_shared "src/basic/missing_stat.h" nm_copy_sd_shared "src/basic/missing_syscall.h" nm_copy_sd_shared "src/basic/missing_type.h" nm_copy_sd_shared "src/basic/ordered-set.c" nm_copy_sd_shared "src/basic/ordered-set.h" nm_copy_sd_shared "src/basic/parse-util.c" nm_copy_sd_shared "src/basic/parse-util.h" nm_copy_sd_shared "src/basic/path-util.c" nm_copy_sd_shared "src/basic/path-util.h" nm_copy_sd_shared "src/basic/prioq.c" nm_copy_sd_shared "src/basic/prioq.h" nm_copy_sd_shared "src/basic/process-util.c" nm_copy_sd_shared "src/basic/process-util.h" nm_copy_sd_shared "src/basic/random-util.c" nm_copy_sd_shared "src/basic/random-util.h" nm_copy_sd_shared "src/basic/ratelimit.c" nm_copy_sd_shared "src/basic/ratelimit.h" nm_copy_sd_shared "src/basic/set.h" nm_copy_sd_shared "src/basic/signal-util.c" nm_copy_sd_shared "src/basic/signal-util.h" nm_copy_sd_shared "src/basic/siphash24.h" nm_copy_sd_shared "src/basic/socket-util.c" nm_copy_sd_shared "src/basic/socket-util.h" nm_copy_sd_shared "src/basic/sort-util.h" nm_copy_sd_shared "src/basic/sparse-endian.h" nm_copy_sd_shared "src/basic/stat-util.c" nm_copy_sd_shared "src/basic/stat-util.h" nm_copy_sd_shared "src/basic/stdio-util.h" nm_copy_sd_shared "src/basic/string-table.c" nm_copy_sd_shared "src/basic/string-table.h" nm_copy_sd_shared "src/basic/string-util.c" nm_copy_sd_shared "src/basic/string-util.h" nm_copy_sd_shared "src/basic/strv.c" nm_copy_sd_shared "src/basic/strv.h" nm_copy_sd_shared "src/basic/strxcpyx.c" nm_copy_sd_shared "src/basic/strxcpyx.h" nm_copy_sd_shared "src/basic/time-util.c" nm_copy_sd_shared "src/basic/time-util.h" nm_copy_sd_shared "src/basic/tmpfile-util.c" nm_copy_sd_shared "src/basic/tmpfile-util.h" nm_copy_sd_shared "src/basic/umask-util.h" nm_copy_sd_shared "src/basic/user-util.h" nm_copy_sd_shared "src/basic/utf8.c" nm_copy_sd_shared "src/basic/utf8.h" nm_copy_sd_shared "src/basic/util.c" nm_copy_sd_shared "src/basic/util.h" nm_copy_sd_shared "src/fundamental/macro-fundamental.h" nm_copy_sd_shared "src/fundamental/string-util-fundamental.c" nm_copy_sd_shared "src/fundamental/string-util-fundamental.h" nm_copy_sd_shared "src/fundamental/type.h" nm_copy_sd_shared "src/shared/dns-domain.c" nm_copy_sd_shared "src/shared/dns-domain.h" nm_copy_sd_shared "src/shared/log-link.h" nm_copy_sd_shared "src/shared/web-util.c" nm_copy_sd_shared "src/shared/web-util.h" nm_copy_sd_stdaux "src/basic/unaligned.h"
166 lines
6.4 KiB
C
166 lines
6.4 KiB
C
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
|
#pragma once
|
|
|
|
#include <alloca.h>
|
|
#include <stddef.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "macro.h"
|
|
|
|
#if HAS_FEATURE_MEMORY_SANITIZER
|
|
# include <sanitizer/msan_interface.h>
|
|
#endif
|
|
|
|
typedef void (*free_func_t)(void *p);
|
|
|
|
/* If for some reason more than 4M are allocated on the stack, let's abort immediately. It's better than
|
|
* proceeding and smashing the stack limits. Note that by default RLIMIT_STACK is 8M on Linux. */
|
|
#define ALLOCA_MAX (4U*1024U*1024U)
|
|
|
|
#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
|
|
|
|
#define new0(t, n) ((t*) calloc((n) ?: 1, sizeof(t)))
|
|
|
|
#define newa(t, n) \
|
|
({ \
|
|
size_t _n_ = n; \
|
|
assert(!size_multiply_overflow(sizeof(t), _n_)); \
|
|
assert(sizeof(t)*_n_ <= ALLOCA_MAX); \
|
|
(t*) alloca((sizeof(t)*_n_) ?: 1); \
|
|
})
|
|
|
|
#define newa0(t, n) \
|
|
({ \
|
|
size_t _n_ = n; \
|
|
assert(!size_multiply_overflow(sizeof(t), _n_)); \
|
|
assert(sizeof(t)*_n_ <= ALLOCA_MAX); \
|
|
(t*) alloca0((sizeof(t)*_n_) ?: 1); \
|
|
})
|
|
|
|
#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
|
|
|
|
#define newdup_suffix0(t, p, n) ((t*) memdup_suffix0_multiply(p, sizeof(t), (n)))
|
|
|
|
#define malloc0(n) (calloc(1, (n) ?: 1))
|
|
|
|
static inline void *mfree(void *memory) {
|
|
free(memory);
|
|
return NULL;
|
|
}
|
|
|
|
#define free_and_replace(a, b) \
|
|
({ \
|
|
free(a); \
|
|
(a) = (b); \
|
|
(b) = NULL; \
|
|
0; \
|
|
})
|
|
|
|
void* memdup(const void *p, size_t l) _alloc_(2);
|
|
void* memdup_suffix0(const void *p, size_t l); /* We can't use _alloc_() here, since we return a buffer one byte larger than the specified size */
|
|
|
|
#define memdupa(p, l) \
|
|
({ \
|
|
void *_q_; \
|
|
size_t _l_ = l; \
|
|
assert(_l_ <= ALLOCA_MAX); \
|
|
_q_ = alloca(_l_ ?: 1); \
|
|
memcpy(_q_, p, _l_); \
|
|
})
|
|
|
|
#define memdupa_suffix0(p, l) \
|
|
({ \
|
|
void *_q_; \
|
|
size_t _l_ = l; \
|
|
assert(_l_ <= ALLOCA_MAX); \
|
|
_q_ = alloca(_l_ + 1); \
|
|
((uint8_t*) _q_)[_l_] = 0; \
|
|
memcpy(_q_, p, _l_); \
|
|
})
|
|
|
|
static inline void freep(void *p) {
|
|
*(void**)p = mfree(*(void**) p);
|
|
}
|
|
|
|
#define _cleanup_free_ _cleanup_(freep)
|
|
|
|
static inline bool size_multiply_overflow(size_t size, size_t need) {
|
|
return _unlikely_(need != 0 && size > (SIZE_MAX / need));
|
|
}
|
|
|
|
_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) {
|
|
if (size_multiply_overflow(size, need))
|
|
return NULL;
|
|
|
|
return malloc(size * need ?: 1);
|
|
}
|
|
|
|
#if !HAVE_REALLOCARRAY
|
|
_alloc_(2, 3) static inline void *reallocarray(void *p, size_t need, size_t size) {
|
|
if (size_multiply_overflow(size, need))
|
|
return NULL;
|
|
|
|
return realloc(p, size * need ?: 1);
|
|
}
|
|
#endif
|
|
|
|
_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, size_t need) {
|
|
if (size_multiply_overflow(size, need))
|
|
return NULL;
|
|
|
|
return memdup(p, size * need);
|
|
}
|
|
|
|
/* Note that we can't decorate this function with _alloc_() since the returned memory area is one byte larger
|
|
* than the product of its parameters. */
|
|
static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t need) {
|
|
if (size_multiply_overflow(size, need))
|
|
return NULL;
|
|
|
|
return memdup_suffix0(p, size * need);
|
|
}
|
|
|
|
void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
|
|
void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
|
|
|
|
#define GREEDY_REALLOC(array, allocated, need) \
|
|
greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
|
|
|
|
#define GREEDY_REALLOC0(array, allocated, need) \
|
|
greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
|
|
|
|
#define alloca0(n) \
|
|
({ \
|
|
char *_new_; \
|
|
size_t _len_ = n; \
|
|
assert(_len_ <= ALLOCA_MAX); \
|
|
_new_ = alloca(_len_ ?: 1); \
|
|
(void *) memset(_new_, 0, _len_); \
|
|
})
|
|
|
|
/* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
|
|
#define alloca_align(size, align) \
|
|
({ \
|
|
void *_ptr_; \
|
|
size_t _mask_ = (align) - 1; \
|
|
size_t _size_ = size; \
|
|
assert(_size_ <= ALLOCA_MAX); \
|
|
_ptr_ = alloca((_size_ + _mask_) ?: 1); \
|
|
(void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
|
|
})
|
|
|
|
#define alloca0_align(size, align) \
|
|
({ \
|
|
void *_new_; \
|
|
size_t _xsize_ = (size); \
|
|
_new_ = alloca_align(_xsize_, (align)); \
|
|
(void*)memset(_new_, 0, _xsize_); \
|
|
})
|
|
|
|
#if HAS_FEATURE_MEMORY_SANITIZER
|
|
# define msan_unpoison(r, s) __msan_unpoison(r, s)
|
|
#else
|
|
# define msan_unpoison(r, s)
|
|
#endif
|