https://github.com/python/cpython/commit/e286845ef1fca5a16c71af1c8742b9b022c7861d
commit: e286845ef1fca5a16c71af1c8742b9b022c7861d
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: zooba <[email protected]>
date: 2026-10-20T18:09:17+01:00
summary:

[3.15] gh-159880: Fix handle leak in winreg when audit hook returns failure 
(GH-159881) (GH-159889)

gh-159880: Fix handle leak in winreg when audit hook returns failure (GH-159881)
(cherry picked from commit af1e0300d90344af4b9d412698e63af67c7f3dcf)

Co-authored-by: An Long <[email protected]>

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 26bcd259efd9879..0a294020d3497fb 100644
--- a/PC/winreg.c
+++ b/PC/winreg.c
@@ -903,6 +903,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;
@@ -955,6 +956,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;
@@ -1401,6 +1403,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]

Reply via email to