bpo-40705: Fix use-after-free in _zoneinfo's module_free (GH-20280)

(cherry picked from commit 06a1b8915d)

Co-authored-by: Ammar Askar <ammar@ammaraskar.com>
This commit is contained in:
Miss Islington (bot) 2020-05-24 07:43:02 -07:00 committed by GitHub
parent 21a9af193c
commit ebf650532b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2490,6 +2490,7 @@ new_weak_cache()
static int static int
initialize_caches() initialize_caches()
{ {
// TODO: Move to a PyModule_GetState / PEP 573 based caching system.
if (TIMEDELTA_CACHE == NULL) { if (TIMEDELTA_CACHE == NULL) {
TIMEDELTA_CACHE = PyDict_New(); TIMEDELTA_CACHE = PyDict_New();
} }
@ -2603,14 +2604,16 @@ module_free()
xdecref_ttinfo(&NO_TTINFO); xdecref_ttinfo(&NO_TTINFO);
Py_XDECREF(TIMEDELTA_CACHE); if (TIMEDELTA_CACHE != NULL && Py_REFCNT(TIMEDELTA_CACHE) > 1) {
if (!Py_REFCNT(TIMEDELTA_CACHE)) { Py_DECREF(TIMEDELTA_CACHE);
TIMEDELTA_CACHE = NULL; } else {
Py_CLEAR(TIMEDELTA_CACHE);
} }
Py_XDECREF(ZONEINFO_WEAK_CACHE); if (ZONEINFO_WEAK_CACHE != NULL && Py_REFCNT(ZONEINFO_WEAK_CACHE) > 1) {
if (!Py_REFCNT(ZONEINFO_WEAK_CACHE)) { Py_DECREF(ZONEINFO_WEAK_CACHE);
ZONEINFO_WEAK_CACHE = NULL; } else {
Py_CLEAR(ZONEINFO_WEAK_CACHE);
} }
strong_cache_free(ZONEINFO_STRONG_CACHE); strong_cache_free(ZONEINFO_STRONG_CACHE);