From 18e1e302452e6dea4500b6f981cee7e151294dea Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Sun, 10 Feb 2019 15:16:20 +0100 Subject: [PATCH] apps/openssl.c: avoid memory leaks The trace API doesn't know that the BIOs we give it, let alone those we attach to callbacks as 'void *data', need to be cleaned up. This must be done in the application. To ensure this cleanup is done as late as possible, use atexit(). Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/8198) --- apps/openssl.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/apps/openssl.c b/apps/openssl.c index 43263295d8..854f943f66 100644 --- a/apps/openssl.c +++ b/apps/openssl.c @@ -163,10 +163,27 @@ static size_t internal_trace_cb(const char *buf, size_t cnt, return ret < 0 ? 0 : ret; } +DEFINE_STACK_OF(tracedata) +static STACK_OF(tracedata) *trace_data_stack; + +static void tracedata_free(tracedata *data) +{ + BIO_free_all(data->bio); + OPENSSL_free(data); +} + +static STACK_OF(tracedata) *trace_data_stack; + +static void cleanup_trace(void) +{ + sk_tracedata_pop_free(trace_data_stack, tracedata_free); +} + static void setup_trace(const char *str) { char *val; + trace_data_stack = sk_tracedata_new_null(); val = OPENSSL_strdup(str); if (val != NULL) { @@ -184,7 +201,10 @@ static void setup_trace(const char *str) if (trace_data == NULL || (trace_data->bio = channel) == NULL || OSSL_trace_set_callback(category, internal_trace_cb, - trace_data) == 0) { + trace_data) == 0 + || sk_tracedata_push(trace_data_stack, trace_data) == 0) { + OSSL_trace_set_callback(category, NULL, NULL); + BIO_free_all(channel); fprintf(stderr, "warning: unable to setup trace callback for category '%s'.\n", item); @@ -198,6 +218,7 @@ static void setup_trace(const char *str) } OPENSSL_free(val); + atexit(cleanup_trace); } int main(int argc, char *argv[])