diff options
author | David Wragg <david@rabbitmq.com> | 2010-07-27 19:36:36 +0100 |
---|---|---|
committer | David Wragg <david@rabbitmq.com> | 2010-07-27 19:36:36 +0100 |
commit | 4f30d164910f88f181489f591c0957317142da01 (patch) | |
tree | 2efeabc9c912f7471c0a0cf95d06a5544c7ac1a0 | |
parent | a39a656ffb1a72b031aa586a37ff77b39939b6fb (diff) | |
download | rabbitmq-c-github-ask-4f30d164910f88f181489f591c0957317142da01.tar.gz |
Free heap-allocated error strings
Even though we are about to exit anyway.
-rw-r--r-- | examples/example_utils.c | 4 | ||||
-rw-r--r-- | tests/test_tables.c | 10 | ||||
-rw-r--r-- | tools/common.c | 19 |
3 files changed, 22 insertions, 11 deletions
diff --git a/examples/example_utils.c b/examples/example_utils.c index ae8f093..48f21f9 100644 --- a/examples/example_utils.c +++ b/examples/example_utils.c @@ -61,7 +61,9 @@ void die_on_error(int x, char const *context) { if (x < 0) { - fprintf(stderr, "%s: %s\n", context, amqp_error_string(-x)); + char *errstr = amqp_error_string(-x); + fprintf(stderr, "%s: %s\n", context, errstr); + free(errstr); exit(1); } } diff --git a/tests/test_tables.c b/tests/test_tables.c index 1a0652a..9c8a041 100644 --- a/tests/test_tables.c +++ b/tests/test_tables.c @@ -210,8 +210,9 @@ static void test_table_codec(void) { int decoding_offset = 0; result = amqp_decode_table(decoding_bytes, &pool, &decoded, &decoding_offset); if (result < 0) { - printf("Table decoding failed: %d (%s)\n", result, - amqp_error_string(-result)); + char *errstr = amqp_error_string(-result); + printf("Table decoding failed: %d (%s)\n", result, errstr); + free(errstr); abort(); } printf("BBBBBBBBBB\n"); @@ -229,8 +230,9 @@ static void test_table_codec(void) { result = amqp_encode_table(encoding_result, &table, &offset); if (result < 0) { - printf("Table encoding failed: %d (%s)\n", result, - amqp_error_string(-result)); + char *errstr = amqp_error_string(-result); + printf("Table encoding failed: %d (%s)\n", result, errstr); + free(errstr); abort(); } diff --git a/tools/common.c b/tools/common.c index 1d8fbd8..2a640be 100644 --- a/tools/common.c +++ b/tools/common.c @@ -77,10 +77,11 @@ void die(const char *fmt, ...) void die_errno(int err, const char *fmt, ...) { + va_list ap; + if (err == 0) return; - va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); @@ -90,14 +91,17 @@ void die_errno(int err, const char *fmt, ...) void die_amqp_error(int err, const char *fmt, ...) { + va_list ap; + char *errstr; + if (err <= 0) return; - - va_list ap; + va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); - fprintf(stderr, ": %s\n", amqp_error_string(err)); + fprintf(stderr, ": %s\n", errstr = amqp_error_string(err)); + free(errstr); exit(1); } @@ -158,14 +162,17 @@ const char *amqp_rpc_reply_string(amqp_rpc_reply_t r) void die_rpc(amqp_rpc_reply_t r, const char *fmt, ...) { + va_list ap; + char *errstr; + if (r.reply_type == AMQP_RESPONSE_NORMAL) return; - va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); - fprintf(stderr, ": %s\n", amqp_rpc_reply_string(r)); + fprintf(stderr, ": %s\n", errstr = amqp_rpc_reply_string(r)); + free(errstr); exit(1); } |