https://github.com/python/cpython/commit/103a384bfdeafc68ab39ea9bf8838a8b2eec83dd
commit: 103a384bfdeafc68ab39ea9bf8838a8b2eec83dd
branch: main
author: Yongtao Huang <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-01-13T19:01:24+05:30
summary:

gh-143249: Fix buffer leak when overlapped operation fails to start on windows 
(#143250)

files:
A Misc/NEWS.d/next/Library/2025-12-28-14-41-02.gh-issue-143249.K4vEp4.rst
M Lib/test/test_asyncio/test_windows_utils.py
M Modules/overlapped.c

diff --git a/Lib/test/test_asyncio/test_windows_utils.py 
b/Lib/test/test_asyncio/test_windows_utils.py
index 97f078ff911b5a..f9ee2f4f68150a 100644
--- a/Lib/test/test_asyncio/test_windows_utils.py
+++ b/Lib/test/test_asyncio/test_windows_utils.py
@@ -129,5 +129,25 @@ def test_popen(self):
             pass
 
 
+class OverlappedRefleakTests(unittest.TestCase):
+
+    def test_wsasendto_failure(self):
+        ov = _overlapped.Overlapped()
+        buf = bytearray(4096)
+        with self.assertRaises(OSError):
+            ov.WSASendTo(0x1234, buf, 0, ("127.0.0.1", 1))
+
+    def test_wsarecvfrom_failure(self):
+        ov = _overlapped.Overlapped()
+        with self.assertRaises(OSError):
+            ov.WSARecvFrom(0x1234, 1024, 0)
+
+    def test_wsarecvfrominto_failure(self):
+        ov = _overlapped.Overlapped()
+        buf = bytearray(4096)
+        with self.assertRaises(OSError):
+            ov.WSARecvFromInto(0x1234, buf, len(buf), 0)
+
+
 if __name__ == '__main__':
     unittest.main()
diff --git 
a/Misc/NEWS.d/next/Library/2025-12-28-14-41-02.gh-issue-143249.K4vEp4.rst 
b/Misc/NEWS.d/next/Library/2025-12-28-14-41-02.gh-issue-143249.K4vEp4.rst
new file mode 100644
index 00000000000000..d50d9e3db850bd
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-12-28-14-41-02.gh-issue-143249.K4vEp4.rst
@@ -0,0 +1 @@
+Fix possible buffer leaks in Windows overlapped I/O on error handling.
diff --git a/Modules/overlapped.c b/Modules/overlapped.c
index 29b7b356648a53..09b57ce4b9773a 100644
--- a/Modules/overlapped.c
+++ b/Modules/overlapped.c
@@ -1806,7 +1806,7 @@ _overlapped_Overlapped_WSASendTo_impl(OverlappedObject 
*self, HANDLE handle,
         case ERROR_IO_PENDING:
             Py_RETURN_NONE;
         default:
-            self->type = TYPE_NOT_STARTED;
+            Overlapped_clear(self);
             return SetFromWindowsErr(err);
     }
 }
@@ -1873,7 +1873,7 @@ _overlapped_Overlapped_WSARecvFrom_impl(OverlappedObject 
*self,
     case ERROR_IO_PENDING:
         Py_RETURN_NONE;
     default:
-        self->type = TYPE_NOT_STARTED;
+        Overlapped_clear(self);
         return SetFromWindowsErr(err);
     }
 }
@@ -1940,7 +1940,7 @@ 
_overlapped_Overlapped_WSARecvFromInto_impl(OverlappedObject *self,
     case ERROR_IO_PENDING:
         Py_RETURN_NONE;
     default:
-        self->type = TYPE_NOT_STARTED;
+        Overlapped_clear(self);
         return SetFromWindowsErr(err);
     }
 }

_______________________________________________
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