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

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

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 159df46bc639d66..4c7d51a6bc4b398 100644
--- a/PC/winreg.c
+++ b/PC/winreg.c
@@ -944,6 +944,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;
@@ -996,6 +997,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;
@@ -1442,6 +1444,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