From ca2d37841476e6272c9957c3f5a0cbe869a531ca Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 10 Apr 2014 09:48:59 -0400 Subject: [PATCH] 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. --- src/journal/coredump.c | 30 +++++++++++++----------------- src/journal/journal-remote.c | 13 ++++++------- src/login/logind-seat.c | 2 +- src/shared/fileio.c | 18 +++++++++--------- src/shared/util.c | 17 +++++++++-------- src/shared/util.h | 22 +++++++--------------- src/systemctl/systemctl.c | 4 +--- 7 files changed, 46 insertions(+), 60 deletions(-) diff --git a/src/journal/coredump.c b/src/journal/coredump.c index 29342de68..74b1dbd01 100644 --- a/src/journal/coredump.c +++ b/src/journal/coredump.c @@ -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; - } + 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; + } - memcpy(coredump_data, "COREDUMP=", 9); - coredump_size = 9; + if (coredump_size == 0) { + memcpy(coredump_data, "COREDUMP=", 9); + coredump_size = 9; + } - for (;;) { 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; diff --git a/src/journal/journal-remote.c b/src/journal/journal-remote.c index 4ece14ee7..794298fe6 100644 --- a/src/journal/journal-remote.c +++ b/src/journal/journal-remote.c @@ -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); diff --git a/src/login/logind-seat.c b/src/login/logind-seat.c index 5350e7700..3114de84d 100644 --- a/src/login/logind-seat.c +++ b/src/login/logind-seat.c @@ -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); diff --git a/src/shared/fileio.c b/src/shared/fileio.c index f10126954..c7b2cd85b 100644 --- a/src/shared/fileio.c +++ b/src/shared/fileio.c @@ -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; } diff --git a/src/shared/util.c b/src/shared/util.c index ffe6624f9..1a727ca40 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -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; } diff --git a/src/shared/util.h b/src/shared/util.h index 90464c940..900f1cf54 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -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)) - -#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; \ - }) +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])) static inline void _reset_errno_(int *saved_errno) { errno = *saved_errno; diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 653a3247b..8fd7bc960 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -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", -- 2.30.2