From 996a47470aa972410a15989ca8ecb3901056396f Mon Sep 17 00:00:00 2001 From: Alan Antonuk Date: Fri, 14 Jun 2013 11:17:18 -0700 Subject: Preserve API/ABI of amqp_error_string() Preserve API/ABI of amqp_error_string() by having it return a string allocated on the heap. Deprecate this function in favor of amqp_error_string2() which returns a statically allocated string --- examples/utils.c | 4 ++-- librabbitmq/amqp.h | 17 +++++++++++++++-- librabbitmq/amqp_api.c | 7 ++++++- tools/common.c | 4 ++-- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/examples/utils.c b/examples/utils.c index 5d4a1f9..ef04968 100644 --- a/examples/utils.c +++ b/examples/utils.c @@ -59,7 +59,7 @@ void die(const char *fmt, ...) void die_on_error(int x, char const *context) { if (x < 0) { - fprintf(stderr, "%s: %s\n", context, amqp_error_string(x)); + fprintf(stderr, "%s: %s\n", context, amqp_error_string2(x)); exit(1); } } @@ -75,7 +75,7 @@ void die_on_amqp_error(amqp_rpc_reply_t x, char const *context) break; case AMQP_RESPONSE_LIBRARY_EXCEPTION: - fprintf(stderr, "%s: %s\n", context, amqp_error_string(x.library_error)); + fprintf(stderr, "%s: %s\n", context, amqp_error_string2(x.library_error)); break; case AMQP_RESPONSE_SERVER_EXCEPTION: diff --git a/librabbitmq/amqp.h b/librabbitmq/amqp.h index 2a0bf19..2020549 100644 --- a/librabbitmq/amqp.h +++ b/librabbitmq/amqp.h @@ -588,11 +588,24 @@ AMQP_CALL amqp_data_in_buffer(amqp_connection_state_t state); /* * Get the error string for the given error code. * - * Error string is statically allocated. (API changed in v0.4.0) + * @deprecated This function has been deprecated in favor of + * \ref amqp_error_string2() which returns statically allocated + * string which do not need to be freed by the caller. + * + * The returned string resides on the heap; the caller is responsible + * for freeing it + * */ +AMQP_DEPRECATED( + AMQP_PUBLIC_FUNCTION + char * + AMQP_CALL amqp_error_string(int err) +); + AMQP_PUBLIC_FUNCTION const char * -AMQP_CALL amqp_error_string(int err); +AMQP_CALL amqp_error_string2(int err); + AMQP_PUBLIC_FUNCTION int diff --git a/librabbitmq/amqp_api.c b/librabbitmq/amqp_api.c index 9f65c4c..8be44ad 100644 --- a/librabbitmq/amqp_api.c +++ b/librabbitmq/amqp_api.c @@ -85,7 +85,7 @@ static const char *ssl_error_strings[] = { static const char *unknown_error_string = "(unknown error)"; -const char *amqp_error_string(int code) +const char *amqp_error_string2(int code) { const char *error_string; size_t category = (((-code) & ERROR_CATEGORY_MASK) >> 16); @@ -126,6 +126,11 @@ const char *amqp_error_string(int code) return error_string; } +char *amqp_error_string(int code) +{ + return strdup(amqp_error_string2(code)); +} + void amqp_abort(const char *fmt, ...) { va_list ap; diff --git a/tools/common.c b/tools/common.c index b556988..a624105 100644 --- a/tools/common.c +++ b/tools/common.c @@ -91,7 +91,7 @@ void die_amqp_error(int err, const char *fmt, ...) va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); - fprintf(stderr, ": %s\n", amqp_error_string(err)); + fprintf(stderr, ": %s\n", amqp_error_string2(err)); exit(1); } @@ -140,7 +140,7 @@ const char *amqp_rpc_reply_string(amqp_rpc_reply_t r) return "missing RPC reply type"; case AMQP_RESPONSE_LIBRARY_EXCEPTION: - return amqp_error_string(r.library_error); + return amqp_error_string2(r.library_error); case AMQP_RESPONSE_SERVER_EXCEPTION: return amqp_server_exception_string(r); -- cgit v1.2.1