Repository: qpid-proton Updated Branches: refs/heads/master 30efcdbea -> d39f22283
Add some NULL checks and fix some realloc leaks Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/c31b0686 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/c31b0686 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/c31b0686 Branch: refs/heads/master Commit: c31b068656579b3b6e47a44bd97b40001f2a66ca Parents: b482e77 Author: dcristoloveanu <[email protected]> Authored: Wed Apr 29 17:15:51 2015 -0700 Committer: dcristoloveanu <[email protected]> Committed: Wed Apr 29 17:15:51 2015 -0700 ---------------------------------------------------------------------- proton-c/src/buffer.c | 34 +++++++++++++++++------- proton-c/src/error.c | 8 +++--- proton-c/src/messenger/store.c | 22 +++++++++------- proton-c/src/object/map.c | 12 +++++---- proton-c/src/object/object.c | 9 ++++--- proton-c/src/parser.c | 10 ++++--- proton-c/src/sasl/none_sasl.c | 48 ++++++++++++++++++---------------- tests/tools/apps/c/reactor-recv.c | 8 ++++-- 8 files changed, 91 insertions(+), 60 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/c31b0686/proton-c/src/buffer.c ---------------------------------------------------------------------- diff --git a/proton-c/src/buffer.c b/proton-c/src/buffer.c index e37aa75..145292a 100644 --- a/proton-c/src/buffer.c +++ b/proton-c/src/buffer.c @@ -40,10 +40,21 @@ struct pn_buffer_t { pn_buffer_t *pn_buffer(size_t capacity) { pn_buffer_t *buf = (pn_buffer_t *) malloc(sizeof(pn_buffer_t)); - buf->capacity = capacity; - buf->start = 0; - buf->size = 0; - buf->bytes = capacity ? (char *) malloc(capacity) : NULL; + if (buf != NULL) { + buf->capacity = capacity; + buf->start = 0; + buf->size = 0; + if (capacity > 0) { + buf->bytes = (char *)malloc(capacity); + if (buf->bytes == NULL) { + free(buf); + buf = NULL; + } + } + else { + buf->bytes = NULL; + } + } return buf; } @@ -135,12 +146,15 @@ int pn_buffer_ensure(pn_buffer_t *buf, size_t size) } if (buf->capacity != old_capacity) { - buf->bytes = (char *) realloc(buf->bytes, buf->capacity); - - if (wrapped) { - size_t n = old_capacity - old_head; - memmove(buf->bytes + buf->capacity - n, buf->bytes + old_head, n); - buf->start = buf->capacity - n; + char* new_bytes = (char *)realloc(buf->bytes, buf->capacity); + if (new_bytes) { + buf->bytes = new_bytes; + + if (wrapped) { + size_t n = old_capacity - old_head; + memmove(buf->bytes + buf->capacity - n, buf->bytes + old_head, n); + buf->start = buf->capacity - n; + } } } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/c31b0686/proton-c/src/error.c ---------------------------------------------------------------------- diff --git a/proton-c/src/error.c b/proton-c/src/error.c index c3cf36a..bbcdf7d 100644 --- a/proton-c/src/error.c +++ b/proton-c/src/error.c @@ -35,9 +35,11 @@ struct pn_error_t { pn_error_t *pn_error() { pn_error_t *error = (pn_error_t *) malloc(sizeof(pn_error_t)); - error->code = 0; - error->text = NULL; - error->root = NULL; + if (error != NULL) { + error->code = 0; + error->text = NULL; + error->root = NULL; + } return error; } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/c31b0686/proton-c/src/messenger/store.c ---------------------------------------------------------------------- diff --git a/proton-c/src/messenger/store.c b/proton-c/src/messenger/store.c index 83b9b68..bdd7e24 100644 --- a/proton-c/src/messenger/store.c +++ b/proton-c/src/messenger/store.c @@ -117,16 +117,18 @@ pni_stream_t *pni_stream(pni_store_t *store, const char *address, bool create) if (create) { stream = (pni_stream_t *) malloc(sizeof(pni_stream_t)); - stream->store = store; - stream->address = pn_string(address); - stream->stream_head = NULL; - stream->stream_tail = NULL; - stream->next = NULL; - - if (prev) { - prev->next = stream; - } else { - store->streams = stream; + if (stream != NULL) { + stream->store = store; + stream->address = pn_string(address); + stream->stream_head = NULL; + stream->stream_tail = NULL; + stream->next = NULL; + + if (prev) { + prev->next = stream; + } else { + store->streams = stream; + } } } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/c31b0686/proton-c/src/object/map.c ---------------------------------------------------------------------- diff --git a/proton-c/src/object/map.c b/proton-c/src/object/map.c index 1a758a1..35e4d05 100644 --- a/proton-c/src/object/map.c +++ b/proton-c/src/object/map.c @@ -80,11 +80,13 @@ static uintptr_t pn_map_hashcode(void *object) static void pni_map_allocate(pn_map_t *map) { map->entries = (pni_entry_t *) malloc(map->capacity * sizeof (pni_entry_t)); - for (size_t i = 0; i < map->capacity; i++) { - map->entries[i].key = NULL; - map->entries[i].value = NULL; - map->entries[i].next = 0; - map->entries[i].state = PNI_ENTRY_FREE; + if (map->entries != NULL) { + for (size_t i = 0; i < map->capacity; i++) { + map->entries[i].key = NULL; + map->entries[i].value = NULL; + map->entries[i].next = 0; + map->entries[i].state = PNI_ENTRY_FREE; + } } map->size = 0; } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/c31b0686/proton-c/src/object/object.c ---------------------------------------------------------------------- diff --git a/proton-c/src/object/object.c b/proton-c/src/object/object.c index 9c91343..05e38cb 100644 --- a/proton-c/src/object/object.c +++ b/proton-c/src/object/object.c @@ -200,10 +200,13 @@ typedef struct { void *pn_object_new(const pn_class_t *clazz, size_t size) { + void *object = NULL; pni_head_t *head = (pni_head_t *) malloc(sizeof(pni_head_t) + size); - void *object = head + 1; - head->clazz = clazz; - head->refcount = 1; + if (head != NULL) { + object = head + 1; + head->clazz = clazz; + head->refcount = 1; + } return object; } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/c31b0686/proton-c/src/parser.c ---------------------------------------------------------------------- diff --git a/proton-c/src/parser.c b/proton-c/src/parser.c index fccba75..93b6da4 100644 --- a/proton-c/src/parser.c +++ b/proton-c/src/parser.c @@ -38,10 +38,12 @@ struct pn_parser_t { pn_parser_t *pn_parser() { pn_parser_t *parser = (pn_parser_t *) malloc(sizeof(pn_parser_t)); - parser->scanner = pn_scanner(); - parser->atoms = NULL; - parser->size = 0; - parser->capacity = 0; + if (parser != NULL) { + parser->scanner = pn_scanner(); + parser->atoms = NULL; + parser->size = 0; + parser->capacity = 0; + } return parser; } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/c31b0686/proton-c/src/sasl/none_sasl.c ---------------------------------------------------------------------- diff --git a/proton-c/src/sasl/none_sasl.c b/proton-c/src/sasl/none_sasl.c index ff962ac..a2414e1 100644 --- a/proton-c/src/sasl/none_sasl.c +++ b/proton-c/src/sasl/none_sasl.c @@ -69,20 +69,21 @@ pn_sasl_t *pn_sasl(pn_transport_t *transport) { if (!transport->sasl) { pni_sasl_t *sasl = (pni_sasl_t *) malloc(sizeof(pni_sasl_t)); - - sasl->client = !transport->server; - sasl->included_mechanisms = NULL; - sasl->selected_mechanism = NULL; - sasl->send_data = pn_buffer(16); - sasl->recv_data = pn_buffer(16); - sasl->outcome = PN_SASL_NONE; - sasl->sent_init = false; - sasl->rcvd_init = false; - sasl->sent_done = false; - sasl->rcvd_done = false; - sasl->halt = false; - - transport->sasl = sasl; + if (sasl != NULL) { + sasl->client = !transport->server; + sasl->included_mechanisms = NULL; + sasl->selected_mechanism = NULL; + sasl->send_data = pn_buffer(16); + sasl->recv_data = pn_buffer(16); + sasl->outcome = PN_SASL_NONE; + sasl->sent_init = false; + sasl->rcvd_init = false; + sasl->sent_done = false; + sasl->rcvd_done = false; + sasl->halt = false; + + transport->sasl = sasl; + } } // The actual external pn_sasl_t pointer is a pointer to its enclosing pn_transport_t @@ -204,15 +205,16 @@ void pn_sasl_plain(pn_sasl_t *sasl0, const char *username, const char *password) size_t psize = strlen(pass); size_t size = usize + psize + 2; char *iresp = (char *) malloc(size); - - iresp[0] = 0; - memmove(iresp + 1, user, usize); - iresp[usize + 1] = 0; - memmove(iresp + usize + 2, pass, psize); - - pn_sasl_allowed_mechs(sasl0, "PLAIN"); - pn_sasl_send(sasl0, iresp, size); - free(iresp); + if (iresp != NULL) { + iresp[0] = 0; + memmove(iresp + 1, user, usize); + iresp[usize + 1] = 0; + memmove(iresp + usize + 2, pass, psize); + + pn_sasl_allowed_mechs(sasl0, "PLAIN"); + pn_sasl_send(sasl0, iresp, size); + free(iresp); + } } void pn_sasl_done(pn_sasl_t *sasl0, pn_sasl_outcome_t outcome) http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/c31b0686/tests/tools/apps/c/reactor-recv.c ---------------------------------------------------------------------- diff --git a/tests/tools/apps/c/reactor-recv.c b/tests/tools/apps/c/reactor-recv.c index d7b92bc..bdd1cc2 100644 --- a/tests/tools/apps/c/reactor-recv.c +++ b/tests/tools/apps/c/reactor-recv.c @@ -95,12 +95,16 @@ typedef struct { static char *ensure_buffer(char *buf, size_t needed, size_t *actual) { + char* new_buf; // Make room for the largest message seen so far, plus extra for slight changes in metadata content if (needed + 1024 <= *actual) return buf; needed += 2048; - buf = (char *) realloc(buf, needed); - *actual = buf ? needed : 0; + new_buf = (char *) realloc(buf, needed); + if (new_buf != NULL) { + buf = new_buf; + *actual = buf ? needed : 0; + } return buf; } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
