https://github.com/python/cpython/commit/af1e0300d90344af4b9d412698e63af67c7f3dcf
commit: af1e0300d90344af4b9d412698e63af67c7f3dcf
branch: main
author: An Long <[email protected]>
committer: zooba <[email protected]>
date: 2026-10-20T16:32:05+01:00
summary:
gh-159880: Fix handle leak in winreg when audit hook returns failure (GH-159881)
files:
A Misc/NEWS.d/next/Windows/2026-10-20-21-37-34.gh-issue-159880.eKBiLr.rst
M PC/winreg.c
diff --git
a/Misc/NEWS.d/next/Windows/2026-10-20-21-37-34.gh-issue-159880.eKBiLr.rst
b/Misc/NEWS.d/next/Windows/2026-10-20-21-37-34.gh-issue-159880.eKBiLr.rst
new file mode 100644
index 000000000000000..ec3c28c4c4303a5
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2026-10-20-21-37-34.gh-issue-159880.eKBiLr.rst
@@ -0,0 +1,3 @@
+Fix a key handle leak in :func:`winreg.CreateKey`,
+:func:`winreg.CreateKeyEx` and :func:`winreg.OpenKey` when the
+``winreg.OpenKey/result`` audit hook fails.
diff --git a/PC/winreg.c b/PC/winreg.c
index 1fe5020419c3dac..417e9a9843375c5 100644
--- a/PC/winreg.c
+++ b/PC/winreg.c
@@ -900,6 +900,7 @@ winreg_CreateKey_impl(PyObject *module, HKEY key, const
wchar_t *sub_key)
}
if (PySys_Audit("winreg.OpenKey/result", "n",
(Py_ssize_t)retKey) < 0) {
+ RegCloseKey(retKey);
return NULL;
}
return retKey;
@@ -952,6 +953,7 @@ winreg_CreateKeyEx_impl(PyObject *module, HKEY key, const
wchar_t *sub_key,
}
if (PySys_Audit("winreg.OpenKey/result", "n",
(Py_ssize_t)retKey) < 0) {
+ RegCloseKey(retKey);
return NULL;
}
return retKey;
@@ -1398,6 +1400,7 @@ winreg_OpenKey_impl(PyObject *module, HKEY key, const
wchar_t *sub_key,
}
if (PySys_Audit("winreg.OpenKey/result", "n",
(Py_ssize_t)retKey) < 0) {
+ RegCloseKey(retKey);
return NULL;
}
return retKey;
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]