Language: C
Label: safe
CWE: CWE-416 \u2014 Use After Free
Referencing memory after it has been freed can cause a program to crash, use unexpected values, or execute code.

Code:
static void ssl_destroy(dtls_srtp *ds, unsigned idx)
{
    DTLS_LOCK(ds);

    /* Destroy SSL instance */
    if (ds->ossl_ssl[idx]) {
        /**
         * Avoid calling SSL_shutdown() if handshake wasn't completed.
         * OpenSSL 1.0.2f complains if SSL_shutdown() is called during an
         * SSL handshake, while previous versions always return 0.       
         */
        if (SSL_in_init(ds->ossl_ssl[idx]) == 0) {
            SSL_shutdown(ds->ossl_ssl[idx]);
        }
        SSL_free(ds->ossl_ssl[idx]); /* this will also close BIOs */
        ds->ossl_ssl[idx] = NULL;
        /* thus reset the BIOs as well */
        ds->ossl_rbio[idx] = NULL;
        ds->ossl_wbio[idx] = NULL;
    }

    /* Destroy SSL context */
    if (ds->ossl_ctx[idx]) {
        SSL_CTX_free(ds->ossl_ctx[idx]);
        ds->ossl_ctx[idx] = NULL;
    }

    DTLS_UNLOCK(ds);
}