File 0310-crypto-Robustify-the-do-once-initialization.patch of Package erlang

From 93b48f41103a7491ae1c6955ff5ceb5ee15ee666 Mon Sep 17 00:00:00 2001
From: Sverker Eriksson <sverker@erlang.org>
Date: Thu, 17 May 2018 19:29:45 +0200
Subject: [PATCH 2/2] crypto: Robustify the do-once-initialization

Introduce boolean 'library_initialized' that is set once
and never cleared as that is how initialization must be done.

Kept 'library_refc' as it may be interesting for debugging.

Moved the three init_*_types() functions last as those must
only be called once and there were error cases bailing out after them.
---
 lib/crypto/c_src/crypto.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/lib/crypto/c_src/crypto.c b/lib/crypto/c_src/crypto.c
index 9e7e1e81ae..5f10e93728 100644
--- a/lib/crypto/c_src/crypto.c
+++ b/lib/crypto/c_src/crypto.c
@@ -528,6 +528,7 @@ static int zero_terminate(ErlNifBinary bin, char **buf);
 #endif
 
 static int library_refc = 0; /* number of users of this dynamic library */
+static int library_initialized = 0;
 
 static ErlNifFunc nif_funcs[] = {
     {"info_lib", 0, info_lib},
@@ -991,7 +992,7 @@ static int initialize(ErlNifEnv* env, ERL_NIF_TERM load_info)
     }
 #endif
 
-    if (library_refc > 0) {
+    if (library_initialized) {
 	/* Repeated loading of this library (module upgrade).
 	 * Atoms and callbacks are already set, we are done.
 	 */
@@ -1101,10 +1102,6 @@ static int initialize(ErlNifEnv* env, ERL_NIF_TERM load_info)
     atom_password = enif_make_atom(env,"password");
 #endif
 
-    init_digest_types(env);
-    init_cipher_types(env);
-    init_algorithms_types(env);
-
 #ifdef HAVE_DYNAMIC_CRYPTO_LIB
     {
 	void* handle;
@@ -1150,6 +1147,11 @@ static int initialize(ErlNifEnv* env, ERL_NIF_TERM load_info)
     }
 #endif /* OPENSSL_THREADS */
 
+    init_digest_types(env);
+    init_cipher_types(env);
+    init_algorithms_types(env);
+
+    library_initialized = 1;
     return 0;
 }
 
-- 
2.16.3