Skip to content

Commit ebf6505

Browse files
bpo-40705: Fix use-after-free in _zoneinfo's module_free (GH-20280)
(cherry picked from commit 06a1b89) Co-authored-by: Ammar Askar <ammar@ammaraskar.com>
1 parent 21a9af1 commit ebf6505

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Modules/_zoneinfo.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -2490,6 +2490,7 @@ new_weak_cache()
24902490
static int
24912491
initialize_caches()
24922492
{
2493+
// TODO: Move to a PyModule_GetState / PEP 573 based caching system.
24932494
if (TIMEDELTA_CACHE == NULL) {
24942495
TIMEDELTA_CACHE = PyDict_New();
24952496
}
@@ -2603,14 +2604,16 @@ module_free()
26032604

26042605
xdecref_ttinfo(&NO_TTINFO);
26052606

2606-
Py_XDECREF(TIMEDELTA_CACHE);
2607-
if (!Py_REFCNT(TIMEDELTA_CACHE)) {
2608-
TIMEDELTA_CACHE = NULL;
2607+
if (TIMEDELTA_CACHE != NULL && Py_REFCNT(TIMEDELTA_CACHE) > 1) {
2608+
Py_DECREF(TIMEDELTA_CACHE);
2609+
} else {
2610+
Py_CLEAR(TIMEDELTA_CACHE);
26092611
}
26102612

2611-
Py_XDECREF(ZONEINFO_WEAK_CACHE);
2612-
if (!Py_REFCNT(ZONEINFO_WEAK_CACHE)) {
2613-
ZONEINFO_WEAK_CACHE = NULL;
2613+
if (ZONEINFO_WEAK_CACHE != NULL && Py_REFCNT(ZONEINFO_WEAK_CACHE) > 1) {
2614+
Py_DECREF(ZONEINFO_WEAK_CACHE);
2615+
} else {
2616+
Py_CLEAR(ZONEINFO_WEAK_CACHE);
26142617
}
26152618

26162619
strong_cache_free(ZONEINFO_STRONG_CACHE);

0 commit comments

Comments
 (0)