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)
This commit is contained in:
Richard Levitte 2019-02-10 15:16:20 +01:00
parent c699712fa3
commit 18e1e30245

View file

@ -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[])