diff options
author | Richard Levitte <levitte@openssl.org> | 2019-02-10 15:16:20 +0100 |
---|---|---|
committer | Richard Levitte <levitte@openssl.org> | 2019-03-06 11:15:14 +0100 |
commit | 18e1e302452e6dea4500b6f981cee7e151294dea (patch) | |
tree | 51eef7e533e7f3028ce83692ae1b6f4ddedbf378 | |
parent | Add a log about the tracing functionality (diff) | |
download | openssl-18e1e302452e6dea4500b6f981cee7e151294dea.tar.xz openssl-18e1e302452e6dea4500b6f981cee7e151294dea.zip |
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 <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/8198)
-rw-r--r-- | apps/openssl.c | 23 |
1 files changed, 22 insertions, 1 deletions
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[]) |