Fix leak in ERR_get_state() when OPENSSL_init_crypto() isn't called yet
[openssl.git] / crypto / err / err.c
index bd9e062fd1bee03a8f1a0260945e640855944981..c0bdbb8933549399bb3cc910b09fc998e2cadf23 100644 (file)
@@ -667,6 +667,14 @@ ERR_STATE *ERR_get_state(void)
     if (!RUN_ONCE(&err_init, err_do_init))
         return NULL;
 
     if (!RUN_ONCE(&err_init, err_do_init))
         return NULL;
 
+    /*
+     * If base OPENSSL_init_crypto() hasn't been called yet, be sure to call
+     * it now to avoid state to be doubly allocated and thereby leak memory.
+     * Needed on any platform that doesn't define OPENSSL_USE_NODELETE.
+     */
+    if (!OPENSSL_init_crypto(0, NULL))
+        return NULL;
+
     state = CRYPTO_THREAD_get_local(&err_thread_local);
 
     if (state == NULL) {
     state = CRYPTO_THREAD_get_local(&err_thread_local);
 
     if (state == NULL) {