Skip to content

Commit 90fa950

Browse files
committed
In the _hashlib module, only initialize the static data for OpenSSL's
constructors once, to avoid memory leaks when finalizing and re-initializing the Python interpreter.
2 parents b2da96b + aded2e5 commit 90fa950

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

‎Modules/_hashopenssl.c‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static PyTypeObject EVPtype;
7070

7171

7272
#define DEFINE_CONSTS_FOR_NEW(Name) \
73-
static PyObject *CONST_ ## Name ## _name_obj; \
73+
static PyObject *CONST_ ## Name ## _name_obj = NULL; \
7474
static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
7575
static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
7676

@@ -585,12 +585,15 @@ generate_hash_name_list(void)
585585
" hash object; optionally initialized with a string") \
586586
}
587587

588-
/* used in the init function to setup a constructor */
588+
/* used in the init function to setup a constructor: initialize OpenSSL
589+
constructor constants if they haven't been initialized already. */
589590
#define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
590-
CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
591-
if (EVP_get_digestbyname(#NAME)) { \
592-
CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
593-
EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
591+
if (CONST_ ## NAME ## _name_obj == NULL) { \
592+
CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
593+
if (EVP_get_digestbyname(#NAME)) { \
594+
CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
595+
EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
596+
} \
594597
} \
595598
} while (0);
596599

0 commit comments

Comments
 (0)