summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-05-24 14:43:02 (GMT)
committerGitHub <noreply@github.com>2020-05-24 14:43:02 (GMT)
commitebf650532b41f5e64a5620b8e47acc3a99555e14 (patch)
treec287da44dc482a43d76fcceee0f3d4ea46b75f4f /Modules
parent21a9af193c35394f560d9517e7a4b579f40b8ef7 (diff)
downloadcpython-ebf650532b41f5e64a5620b8e47acc3a99555e14.zip
cpython-ebf650532b41f5e64a5620b8e47acc3a99555e14.tar.gz
cpython-ebf650532b41f5e64a5620b8e47acc3a99555e14.tar.bz2
bpo-40705: Fix use-after-free in _zoneinfo's module_free (GH-20280)
(cherry picked from commit 06a1b8915d6674e40f0dccc422ca2c06212392d8) Co-authored-by: Ammar Askar <ammar@ammaraskar.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_zoneinfo.c15
1 files changed, 9 insertions, 6 deletions
diff --git a/Modules/_zoneinfo.c b/Modules/_zoneinfo.c
index d7e7157..d852c76 100644
--- a/Modules/_zoneinfo.c
+++ b/Modules/_zoneinfo.c
@@ -2490,6 +2490,7 @@ new_weak_cache()
static int
initialize_caches()
{
+ // TODO: Move to a PyModule_GetState / PEP 573 based caching system.
if (TIMEDELTA_CACHE == NULL) {
TIMEDELTA_CACHE = PyDict_New();
}
@@ -2603,14 +2604,16 @@ module_free()
xdecref_ttinfo(&NO_TTINFO);
- Py_XDECREF(TIMEDELTA_CACHE);
- if (!Py_REFCNT(TIMEDELTA_CACHE)) {
- TIMEDELTA_CACHE = NULL;
+ if (TIMEDELTA_CACHE != NULL && Py_REFCNT(TIMEDELTA_CACHE) > 1) {
+ Py_DECREF(TIMEDELTA_CACHE);
+ } else {
+ Py_CLEAR(TIMEDELTA_CACHE);
}
- Py_XDECREF(ZONEINFO_WEAK_CACHE);
- if (!Py_REFCNT(ZONEINFO_WEAK_CACHE)) {
- ZONEINFO_WEAK_CACHE = NULL;
+ if (ZONEINFO_WEAK_CACHE != NULL && Py_REFCNT(ZONEINFO_WEAK_CACHE) > 1) {
+ Py_DECREF(ZONEINFO_WEAK_CACHE);
+ } else {
+ Py_CLEAR(ZONEINFO_WEAK_CACHE);
}
strong_cache_free(ZONEINFO_STRONG_CACHE);