From 25217e73205dd8f9f58c026e40ba07a2bd5e1add Mon Sep 17 00:00:00 2001 From: Imgyu Kim Date: Sat, 21 Mar 2026 17:40:01 +0900 Subject: [PATCH] gh-146245: Fix ref/buffer leaks in socketmodule.c on audit hook failure Fix two leak bugs in socketmodule.c when PySys_Audit raises: 1. getaddrinfo: 'return NULL' replaced with 'goto err' to ensure idna and pstr refs are properly released via Py_XDECREF. 2. sock_sendto: Added PyBuffer_Release(&pbuf) before 'return NULL' to release the Py_buffer when audit hook raises. --- .../Library/2026-03-21-00-00-00.gh-issue-146245.SockAud.rst | 3 +++ Modules/socketmodule.c | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-03-21-00-00-00.gh-issue-146245.SockAud.rst diff --git a/Misc/NEWS.d/next/Library/2026-03-21-00-00-00.gh-issue-146245.SockAud.rst b/Misc/NEWS.d/next/Library/2026-03-21-00-00-00.gh-issue-146245.SockAud.rst new file mode 100644 index 00000000000000..bb0afdef3c5146 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-21-00-00-00.gh-issue-146245.SockAud.rst @@ -0,0 +1,3 @@ +Fix reference leak of ``idna`` and ``pstr`` in :func:`socket.getaddrinfo` +and buffer leak in :meth:`socket.socket.sendto` when ``PySys_Audit`` +raises an exception. diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index d4df40c78e8a4f..8215d51baf30e8 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -4808,6 +4808,7 @@ sock_sendto(PyObject *self, PyObject *args) } if (PySys_Audit("socket.sendto", "OO", s, addro) < 0) { + PyBuffer_Release(&pbuf); return NULL; } @@ -6982,7 +6983,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) if (PySys_Audit("socket.getaddrinfo", "OOiii", hobj, pobj, family, socktype, protocol) < 0) { - return NULL; + goto err; } memset(&hints, 0, sizeof(hints));