summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTony Garnock-Jones <tonyg@kcbbs.gen.nz>2009-04-29 23:02:17 +0100
committerTony Garnock-Jones <tonyg@kcbbs.gen.nz>2009-04-29 23:02:17 +0100
commit92ec7c66b268b922d28f089125ab76a466cdd657 (patch)
tree6cade7c7e4c8b27fbf5c1daaebc5fb4f0cafac8f
parent720be63044d57282e7e5bb5b88195639d654c94f (diff)
downloadrabbitmq-c-92ec7c66b268b922d28f089125ab76a466cdd657.tar.gz
Be more careful about checking allocation results.
-rw-r--r--examples/amqp_listen.c4
-rw-r--r--librabbitmq/amqp.h4
-rw-r--r--librabbitmq/amqp_connection.c31
-rw-r--r--librabbitmq/amqp_mem.c31
-rw-r--r--librabbitmq/amqp_table.c11
5 files changed, 69 insertions, 12 deletions
diff --git a/examples/amqp_listen.c b/examples/amqp_listen.c
index b67474d..e33b3ef 100644
--- a/examples/amqp_listen.c
+++ b/examples/amqp_listen.c
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <errno.h>
#include <stdint.h>
#include <amqp.h>
@@ -59,6 +60,9 @@ int main(int argc, char const * const *argv) {
"Declaring queue");
amqp_queue_declare_ok_t *r = (amqp_queue_declare_ok_t *) result.reply.decoded;
queuename = amqp_bytes_malloc_dup(r->queue);
+ if (queuename.bytes == NULL) {
+ die_on_error(-ENOMEM, "Copying queue name");
+ }
}
{
diff --git a/librabbitmq/amqp.h b/librabbitmq/amqp.h
index 03b682f..c5314fb 100644
--- a/librabbitmq/amqp.h
+++ b/librabbitmq/amqp.h
@@ -107,8 +107,8 @@ extern amqp_bytes_t amqp_bytes_malloc_dup(amqp_bytes_t src);
extern amqp_connection_state_t amqp_new_connection(void);
extern void amqp_set_sockfd(amqp_connection_state_t state,
int sockfd);
-extern void amqp_tune_connection(amqp_connection_state_t state,
- int frame_max);
+extern int amqp_tune_connection(amqp_connection_state_t state,
+ int frame_max);
extern void amqp_destroy_connection(amqp_connection_state_t state);
extern int amqp_handle_input(amqp_connection_state_t state,
diff --git a/librabbitmq/amqp_connection.c b/librabbitmq/amqp_connection.c
index 57db2c3..baaff87 100644
--- a/librabbitmq/amqp_connection.c
+++ b/librabbitmq/amqp_connection.c
@@ -31,6 +31,10 @@ amqp_connection_state_t amqp_new_connection(void) {
amqp_connection_state_t state =
(amqp_connection_state_t) calloc(1, sizeof(struct amqp_connection_state_t_));
+ if (state == NULL) {
+ return NULL;
+ }
+
init_amqp_pool(&state->frame_pool, INITIAL_FRAME_POOL_PAGE_SIZE);
init_amqp_pool(&state->decoding_pool, INITIAL_DECODING_POOL_PAGE_SIZE);
@@ -38,7 +42,12 @@ amqp_connection_state_t amqp_new_connection(void) {
state->inbound_buffer.bytes = NULL;
state->outbound_buffer.bytes = NULL;
- amqp_tune_connection(state, INITIAL_FRAME_POOL_PAGE_SIZE);
+ if (amqp_tune_connection(state, INITIAL_FRAME_POOL_PAGE_SIZE) != 0) {
+ empty_amqp_pool(&state->frame_pool);
+ empty_amqp_pool(&state->decoding_pool);
+ free(state);
+ return NULL;
+ }
state->inbound_offset = 0;
state->target_size = HEADER_SIZE;
@@ -46,6 +55,11 @@ amqp_connection_state_t amqp_new_connection(void) {
state->sockfd = -1;
state->sock_inbound_buffer.len = INITIAL_INBOUND_SOCK_BUFFER_SIZE;
state->sock_inbound_buffer.bytes = malloc(INITIAL_INBOUND_SOCK_BUFFER_SIZE);
+ if (state->sock_inbound_buffer.bytes == NULL) {
+ amqp_destroy_connection(state);
+ return NULL;
+ }
+
state->sock_inbound_offset = 0;
state->sock_inbound_limit = 0;
@@ -61,9 +75,11 @@ void amqp_set_sockfd(amqp_connection_state_t state,
state->sockfd = sockfd;
}
-void amqp_tune_connection(amqp_connection_state_t state,
- int frame_max)
+int amqp_tune_connection(amqp_connection_state_t state,
+ int frame_max)
{
+ void *newbuf;
+
ENFORCE_STATE(state, CONNECTION_STATE_IDLE);
state->frame_max = frame_max;
@@ -73,7 +89,14 @@ void amqp_tune_connection(amqp_connection_state_t state,
state->inbound_buffer.len = frame_max;
state->outbound_buffer.len = frame_max;
- state->outbound_buffer.bytes = realloc(state->outbound_buffer.bytes, frame_max);
+ newbuf = realloc(state->outbound_buffer.bytes, frame_max);
+ if (newbuf == NULL) {
+ amqp_destroy_connection(state);
+ return -ENOMEM;
+ }
+ state->outbound_buffer.bytes = newbuf;
+
+ return 0;
}
void amqp_destroy_connection(amqp_connection_state_t state) {
diff --git a/librabbitmq/amqp_mem.c b/librabbitmq/amqp_mem.c
index 79d7e08..0e84dd7 100644
--- a/librabbitmq/amqp_mem.c
+++ b/librabbitmq/amqp_mem.c
@@ -3,6 +3,7 @@
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
+#include <errno.h>
#include <assert.h>
#include "amqp.h"
@@ -51,17 +52,25 @@ void empty_amqp_pool(amqp_pool_t *pool) {
empty_blocklist(&pool->pages);
}
-static void record_pool_block(amqp_pool_blocklist_t *x, void *block) {
+static int record_pool_block(amqp_pool_blocklist_t *x, void *block) {
size_t blocklistlength = sizeof(void *) * (x->num_blocks + 1);
if (x->blocklist == NULL) {
x->blocklist = malloc(blocklistlength);
+ if (x->blocklist == NULL) {
+ return -ENOMEM;
+ }
} else {
- x->blocklist = realloc(x->blocklist, blocklistlength);
+ void *newbl = realloc(x->blocklist, blocklistlength);
+ if (newbl == NULL) {
+ return -ENOMEM;
+ }
+ x->blocklist = newbl;
}
x->blocklist[x->num_blocks] = block;
x->num_blocks++;
+ return 0;
}
void *amqp_pool_alloc(amqp_pool_t *pool, size_t amount) {
@@ -73,7 +82,12 @@ void *amqp_pool_alloc(amqp_pool_t *pool, size_t amount) {
if (amount > pool->pagesize) {
void *result = calloc(1, amount);
- record_pool_block(&pool->large_blocks, result);
+ if (result == NULL) {
+ return NULL;
+ }
+ if (record_pool_block(&pool->large_blocks, result) != 0) {
+ return NULL;
+ }
return result;
}
@@ -89,7 +103,12 @@ void *amqp_pool_alloc(amqp_pool_t *pool, size_t amount) {
if (pool->next_page >= pool->pages.num_blocks) {
pool->alloc_block = calloc(1, pool->pagesize);
- record_pool_block(&pool->pages, pool->alloc_block);
+ if (pool->alloc_block == NULL) {
+ return NULL;
+ }
+ if (record_pool_block(&pool->pages, pool->alloc_block) != 0) {
+ return NULL;
+ }
pool->next_page = pool->pages.num_blocks;
} else {
pool->alloc_block = pool->pages.blocklist[pool->next_page];
@@ -117,6 +136,8 @@ amqp_bytes_t amqp_bytes_malloc_dup(amqp_bytes_t src) {
amqp_bytes_t result;
result.len = src.len;
result.bytes = malloc(src.len);
- memcpy(result.bytes, src.bytes, src.len);
+ if (result.bytes != NULL) {
+ memcpy(result.bytes, src.bytes, src.len);
+ }
return result;
}
diff --git a/librabbitmq/amqp_table.c b/librabbitmq/amqp_table.c
index d8200e4..3ada5ec 100644
--- a/librabbitmq/amqp_table.c
+++ b/librabbitmq/amqp_table.c
@@ -21,6 +21,10 @@ int amqp_decode_table(amqp_bytes_t encoded,
int allocated_entries = INITIAL_TABLE_SIZE;
int limit;
+ if (entries == NULL) {
+ return -ENOMEM;
+ }
+
offset += 4;
limit = offset + tablesize;
@@ -32,8 +36,13 @@ int amqp_decode_table(amqp_bytes_t encoded,
offset++;
if (num_entries >= allocated_entries) {
+ void *newentries;
allocated_entries = allocated_entries * 2;
- entries = realloc(entries, allocated_entries * sizeof(amqp_table_entry_t));
+ newentries = realloc(entries, allocated_entries * sizeof(amqp_table_entry_t));
+ if (newentries == NULL) {
+ return -ENOMEM;
+ }
+ entries = newentries;
}
entry = &entries[num_entries];