summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorRichard Levitte <levitte@openssl.org>2019-02-10 15:16:20 +0100
committerRichard Levitte <levitte@openssl.org>2019-03-06 11:15:14 +0100
commit18e1e302452e6dea4500b6f981cee7e151294dea (patch)
tree51eef7e533e7f3028ce83692ae1b6f4ddedbf378 /apps
parentc699712fa329ce1ce1b4756a50a78ab2f47909e1 (diff)
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)
Diffstat (limited to 'apps')
-rw-r--r--apps/openssl.c23
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[])