Unify GREEDY_REALLOC and GREEDY_REALLOC_T
greedy_realloc() and greedy_realloc0() now store the allocated size as the count, not bytes. Replace GREEDY_REALLOC uses with GREEDY_REALLOC_T everywhere, and then rename GREEDY_REALLOC_T to GREEDY_REALLOC. It is just too error-prone to have two slightly different macros which do the same thing.
This commit is contained in:
parent
7cc832b91e
commit
ca2d378414
|
@ -39,10 +39,10 @@
|
|||
#include "journald-native.h"
|
||||
|
||||
/* Few programs have less than 3MiB resident */
|
||||
#define COREDUMP_MIN_START (3*1024*1024)
|
||||
#define COREDUMP_MIN_START (3*1024*1024u)
|
||||
/* Make sure to not make this larger than the maximum journal entry
|
||||
* size. See ENTRY_SIZE_MAX in journald-native.c. */
|
||||
#define COREDUMP_MAX (767*1024*1024)
|
||||
#define COREDUMP_MAX (767*1024*1024u)
|
||||
assert_cc(COREDUMP_MAX <= ENTRY_SIZE_MAX);
|
||||
|
||||
enum {
|
||||
|
@ -107,7 +107,7 @@ int main(int argc, char* argv[]) {
|
|||
uid_t uid;
|
||||
gid_t gid;
|
||||
struct iovec iovec[14];
|
||||
size_t coredump_bufsize, coredump_size;
|
||||
size_t coredump_bufsize = 0, coredump_size = 0;
|
||||
_cleanup_free_ char *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
|
||||
*core_timestamp = NULL, *core_comm = NULL, *core_exe = NULL, *core_unit = NULL,
|
||||
*core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *coredump_data = NULL;
|
||||
|
@ -239,17 +239,18 @@ int main(int argc, char* argv[]) {
|
|||
goto finish;
|
||||
}
|
||||
|
||||
coredump_bufsize = COREDUMP_MIN_START;
|
||||
coredump_data = malloc(coredump_bufsize);
|
||||
if (!coredump_data) {
|
||||
log_warning("Failed to allocate memory for core, core will not be stored.");
|
||||
goto finalize;
|
||||
}
|
||||
|
||||
memcpy(coredump_data, "COREDUMP=", 9);
|
||||
coredump_size = 9;
|
||||
|
||||
for (;;) {
|
||||
if (!GREEDY_REALLOC(coredump_data, coredump_bufsize,
|
||||
MAX(coredump_size + 1, COREDUMP_MIN_START/2))) {
|
||||
log_warning("Failed to allocate memory for core, core will not be stored.");
|
||||
goto finalize;
|
||||
}
|
||||
|
||||
if (coredump_size == 0) {
|
||||
memcpy(coredump_data, "COREDUMP=", 9);
|
||||
coredump_size = 9;
|
||||
}
|
||||
|
||||
n = loop_read(STDIN_FILENO, coredump_data + coredump_size,
|
||||
coredump_bufsize - coredump_size, false);
|
||||
if (n < 0) {
|
||||
|
@ -265,11 +266,6 @@ int main(int argc, char* argv[]) {
|
|||
log_error("Core too large, core will not be stored.");
|
||||
goto finalize;
|
||||
}
|
||||
|
||||
if (!GREEDY_REALLOC(coredump_data, coredump_bufsize, coredump_size + 1)) {
|
||||
log_warning("Failed to allocate memory for core, core will not be stored.");
|
||||
goto finalize;
|
||||
}
|
||||
}
|
||||
|
||||
iovec[j].iov_base = coredump_data;
|
||||
|
|
|
@ -226,8 +226,8 @@ typedef struct MHDDaemonWrapper {
|
|||
|
||||
typedef struct RemoteServer {
|
||||
RemoteSource **sources;
|
||||
ssize_t sources_size;
|
||||
ssize_t active;
|
||||
size_t sources_size;
|
||||
size_t active;
|
||||
|
||||
sd_event *events;
|
||||
sd_event_source *sigterm_event, *sigint_event, *listen_event;
|
||||
|
@ -257,7 +257,7 @@ static int get_source_for_fd(RemoteServer *s, int fd, RemoteSource **source) {
|
|||
assert(fd >= 0);
|
||||
assert(source);
|
||||
|
||||
if (!GREEDY_REALLOC0_T(s->sources, s->sources_size, fd + 1))
|
||||
if (!GREEDY_REALLOC0(s->sources, s->sources_size, fd + 1))
|
||||
return log_oom();
|
||||
|
||||
if (s->sources[fd] == NULL) {
|
||||
|
@ -276,8 +276,7 @@ static int remove_source(RemoteServer *s, int fd) {
|
|||
RemoteSource *source;
|
||||
|
||||
assert(s);
|
||||
assert(fd >= 0);
|
||||
assert(fd < s->sources_size);
|
||||
assert(fd >= 0 && fd < (ssize_t) s->sources_size);
|
||||
|
||||
source = s->sources[fd];
|
||||
if (source) {
|
||||
|
@ -837,7 +836,7 @@ static int remoteserver_init(RemoteServer *s) {
|
|||
|
||||
static int server_destroy(RemoteServer *s) {
|
||||
int r;
|
||||
ssize_t i;
|
||||
size_t i;
|
||||
MHDDaemonWrapper *d;
|
||||
|
||||
r = writer_close(&s->writer);
|
||||
|
@ -879,7 +878,7 @@ static int dispatch_raw_source_event(sd_event_source *event,
|
|||
RemoteSource *source;
|
||||
int r;
|
||||
|
||||
assert(fd < s->sources_size);
|
||||
assert(fd >= 0 && fd < (ssize_t) s->sources_size);
|
||||
source = s->sources[fd];
|
||||
assert(source->fd == fd);
|
||||
|
||||
|
|
|
@ -485,7 +485,7 @@ void seat_claim_position(Seat *s, Session *session, unsigned int pos) {
|
|||
if (seat_has_vts(s))
|
||||
pos = session->vtnr;
|
||||
|
||||
if (!GREEDY_REALLOC0_T(s->positions, s->position_count, pos + 1))
|
||||
if (!GREEDY_REALLOC0(s->positions, s->position_count, pos + 1))
|
||||
return;
|
||||
|
||||
seat_evict_position(s, session);
|
||||
|
|
|
@ -294,7 +294,7 @@ static int parse_env_file_internal(
|
|||
state = KEY;
|
||||
last_key_whitespace = (size_t) -1;
|
||||
|
||||
if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) {
|
||||
if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
|
||||
r = -ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
|
@ -317,7 +317,7 @@ static int parse_env_file_internal(
|
|||
else if (last_key_whitespace == (size_t) -1)
|
||||
last_key_whitespace = n_key;
|
||||
|
||||
if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) {
|
||||
if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
|
||||
r = -ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
|
@ -357,7 +357,7 @@ static int parse_env_file_internal(
|
|||
else if (!strchr(WHITESPACE, c)) {
|
||||
state = VALUE;
|
||||
|
||||
if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
|
||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
||||
r = -ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
|
@ -402,7 +402,7 @@ static int parse_env_file_internal(
|
|||
else if (last_value_whitespace == (size_t) -1)
|
||||
last_value_whitespace = n_value;
|
||||
|
||||
if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
|
||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
||||
r = -ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
|
@ -417,7 +417,7 @@ static int parse_env_file_internal(
|
|||
|
||||
if (!strchr(newline, c)) {
|
||||
/* Escaped newlines we eat up entirely */
|
||||
if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
|
||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
||||
r = -ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
|
@ -432,7 +432,7 @@ static int parse_env_file_internal(
|
|||
else if (c == '\\')
|
||||
state = SINGLE_QUOTE_VALUE_ESCAPE;
|
||||
else {
|
||||
if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
|
||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
||||
r = -ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
|
@ -446,7 +446,7 @@ static int parse_env_file_internal(
|
|||
state = SINGLE_QUOTE_VALUE;
|
||||
|
||||
if (!strchr(newline, c)) {
|
||||
if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
|
||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
||||
r = -ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
|
@ -461,7 +461,7 @@ static int parse_env_file_internal(
|
|||
else if (c == '\\')
|
||||
state = DOUBLE_QUOTE_VALUE_ESCAPE;
|
||||
else {
|
||||
if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
|
||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
||||
r = -ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
|
@ -475,7 +475,7 @@ static int parse_env_file_internal(
|
|||
state = DOUBLE_QUOTE_VALUE;
|
||||
|
||||
if (!strchr(newline, c)) {
|
||||
if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
|
||||
if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
|
||||
r = -ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
|
|
|
@ -5819,8 +5819,8 @@ char *strrep(const char *s, unsigned n) {
|
|||
return r;
|
||||
}
|
||||
|
||||
void* greedy_realloc(void **p, size_t *allocated, size_t need) {
|
||||
size_t a;
|
||||
void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) {
|
||||
size_t a, newalloc;
|
||||
void *q;
|
||||
|
||||
assert(p);
|
||||
|
@ -5829,10 +5829,11 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need) {
|
|||
if (*allocated >= need)
|
||||
return *p;
|
||||
|
||||
a = MAX(64u, need * 2);
|
||||
newalloc = MAX(need * 2, 64u / size);
|
||||
a = newalloc * size;
|
||||
|
||||
/* check for overflows */
|
||||
if (a < need)
|
||||
if (a < size * need)
|
||||
return NULL;
|
||||
|
||||
q = realloc(*p, a);
|
||||
|
@ -5840,11 +5841,11 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need) {
|
|||
return NULL;
|
||||
|
||||
*p = q;
|
||||
*allocated = a;
|
||||
*allocated = newalloc;
|
||||
return q;
|
||||
}
|
||||
|
||||
void* greedy_realloc0(void **p, size_t *allocated, size_t need) {
|
||||
void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size) {
|
||||
size_t prev;
|
||||
uint8_t *q;
|
||||
|
||||
|
@ -5853,12 +5854,12 @@ void* greedy_realloc0(void **p, size_t *allocated, size_t need) {
|
|||
|
||||
prev = *allocated;
|
||||
|
||||
q = greedy_realloc(p, allocated, need);
|
||||
q = greedy_realloc(p, allocated, need, size);
|
||||
if (!q)
|
||||
return NULL;
|
||||
|
||||
if (*allocated > prev)
|
||||
memzero(&q[prev], *allocated - prev);
|
||||
memzero(q + prev * size, (*allocated - prev) * size);
|
||||
|
||||
return q;
|
||||
}
|
||||
|
|
|
@ -730,21 +730,13 @@ void *unhexmem(const char *p, size_t l);
|
|||
char *strextend(char **x, ...) _sentinel_;
|
||||
char *strrep(const char *s, unsigned n);
|
||||
|
||||
void* greedy_realloc(void **p, size_t *allocated, size_t need);
|
||||
void* greedy_realloc0(void **p, size_t *allocated, size_t need);
|
||||
#define GREEDY_REALLOC(array, allocated, need) \
|
||||
greedy_realloc((void**) &(array), &(allocated), sizeof((array)[0]) * (need))
|
||||
#define GREEDY_REALLOC0(array, allocated, need) \
|
||||
greedy_realloc0((void**) &(array), &(allocated), sizeof((array)[0]) * (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_T(array, count, need) \
|
||||
({ \
|
||||
size_t _size = (count) * sizeof((array)[0]); \
|
||||
void *_ptr = GREEDY_REALLOC0((array), _size, (need)); \
|
||||
if (_ptr) \
|
||||
(count) = _size / sizeof((array)[0]); \
|
||||
_ptr; \
|
||||
})
|
||||
#define GREEDY_REALLOC0(array, allocated, need) \
|
||||
greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
|
||||
|
||||
static inline void _reset_errno_(int *saved_errno) {
|
||||
errno = *saved_errno;
|
||||
|
|
|
@ -515,7 +515,7 @@ static int get_unit_list(
|
|||
|
||||
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
||||
size_t size;
|
||||
size_t size = c;
|
||||
int r;
|
||||
UnitInfo u;
|
||||
|
||||
|
@ -523,8 +523,6 @@ static int get_unit_list(
|
|||
assert(unit_infos);
|
||||
assert(_reply);
|
||||
|
||||
size = sizeof(UnitInfo) * c;
|
||||
|
||||
r = sd_bus_call_method(
|
||||
bus,
|
||||
"org.freedesktop.systemd1",
|
||||
|
|
Loading…
Reference in a new issue