From 821e0756c62d705252ab13d24af941578c0b9642 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 16 Dec 2013 20:32:37 +0100 Subject: [PATCH 1/1] bus: use GREEDY_REALLOC() when allocating message queues --- src/libsystemd-bus/bus-internal.h | 4 ++- src/libsystemd-bus/sd-bus.c | 47 ++++++++----------------------- 2 files changed, 15 insertions(+), 36 deletions(-) diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h index a599e5ab8..7af8c1e22 100644 --- a/src/libsystemd-bus/bus-internal.h +++ b/src/libsystemd-bus/bus-internal.h @@ -168,11 +168,13 @@ struct sd_bus { size_t rbuffer_size; sd_bus_message **rqueue; - unsigned rqueue_size, rqueue_allocated; + unsigned rqueue_size; + size_t rqueue_allocated; sd_bus_message **wqueue; unsigned wqueue_size; size_t windex; + size_t wqueue_allocated; uint64_t serial; diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 4eaceeff0..76a27ec3c 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -118,6 +118,7 @@ static void bus_reset_queues(sd_bus *b) { free(b->wqueue); b->rqueue = b->wqueue = NULL; + b->rqueue_allocated = b->wqueue_allocated = 0; b->rqueue_size = b->wqueue_size = 0; } @@ -195,8 +196,7 @@ _public_ int sd_bus_new(sd_bus **ret) { /* We guarantee that wqueue always has space for at least one * entry */ - r->wqueue = new(sd_bus_message*, 1); - if (!r->wqueue) { + if (!GREEDY_REALLOC(r->wqueue, r->wqueue_allocated, 1)) { free(r); return -ENOMEM; } @@ -397,7 +397,7 @@ int bus_start_running(sd_bus *bus) { } static int parse_address_key(const char **p, const char *key, char **value) { - size_t l, n = 0; + size_t l, n = 0, allocated = 0; const char *a; char *r = NULL; @@ -421,7 +421,7 @@ static int parse_address_key(const char **p, const char *key, char **value) { a = *p; while (*a != ';' && *a != ',' && *a != 0) { - char c, *t; + char c; if (*a == '%') { int x, y; @@ -445,13 +445,9 @@ static int parse_address_key(const char **p, const char *key, char **value) { a++; } - t = realloc(r, n + 2); - if (!t) { - free(r); + if (!GREEDY_REALLOC(r, allocated, n + 2)) return -ENOMEM; - } - r = t; r[n++] = c; } @@ -614,6 +610,7 @@ static int parse_exec_address(sd_bus *b, const char **p, char **guid) { char *path = NULL; unsigned n_argv = 0, j; char **argv = NULL; + size_t allocated = 0; int r; assert(b); @@ -647,17 +644,11 @@ static int parse_exec_address(sd_bus *b, const char **p, char **guid) { (*p) ++; if (ul >= n_argv) { - char **x; - - x = realloc(argv, sizeof(char*) * (ul + 2)); - if (!x) { + if (!GREEDY_REALLOC0(argv, allocated, ul + 2)) { r = -ENOMEM; goto fail; } - memset(x + n_argv, 0, sizeof(char*) * (ul - n_argv + 2)); - - argv = x; n_argv = ul + 1; } @@ -1422,24 +1413,14 @@ static int bus_read_message(sd_bus *bus) { } int bus_rqueue_make_room(sd_bus *bus) { - sd_bus_message **q; - unsigned x; - - x = bus->rqueue_size + 1; - - if (bus->rqueue_allocated >= x) - return 0; + assert(bus); - if (x > BUS_RQUEUE_MAX) + if (bus->rqueue_size >= BUS_RQUEUE_MAX) return -ENOBUFS; - q = realloc(bus->rqueue, x * sizeof(sd_bus_message*)); - if (!q) + if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1)) return -ENOMEM; - bus->rqueue = q; - bus->rqueue_allocated = x; - return 0; } @@ -1521,19 +1502,15 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) { bus->windex = idx; } } else { - sd_bus_message **q; - /* Just append it to the queue. */ if (bus->wqueue_size >= BUS_WQUEUE_MAX) return -ENOBUFS; - q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1)); - if (!q) + if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1)) return -ENOMEM; - bus->wqueue = q; - q[bus->wqueue_size ++] = sd_bus_message_ref(m); + bus->wqueue[bus->wqueue_size ++] = sd_bus_message_ref(m); } if (serial) -- 2.30.2