From 2621a3262858f391ffcfb85b7afa18c633d4ca46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Fri, 26 Jul 2024 12:10:23 +0200 Subject: [PATCH 1/3] gh-84808: Handle errors returned by internal_connect() internal_connect() errors when returning -1 with exception set. reported and proposed by Ryan C. Gordon error handling scheme (raise, no raise) as suggested by encukou Co-authored-by: Petr Viktorin --- Modules/socketmodule.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 3ffdaa45f16ac7..aa99e2af712c28 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -3405,6 +3405,18 @@ sock_connect_impl(PySocketSockObject *s, void* Py_UNUSED(data)) return 1; } +/* Common functionality for socket.connect and socket.connect_ex. + * + * If *raise* is set: + * - On success, return 0. + * - On any failure, return -1 with an exception set. + * If *raise* is zero: + * - On success, return 0. + * - On connect() failure, return errno (without an exception set) + * - On other error, return -1 with an exception set. + * + * Note that -1 is a valid errno value on some systems. + */ static int internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, int raise) @@ -3489,8 +3501,10 @@ sock_connect(PySocketSockObject *s, PyObject *addro) } res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 1); - if (res < 0) + if (res < 0) { + assert(PyErr_Occurred()); return NULL; + } Py_RETURN_NONE; } @@ -3520,8 +3534,9 @@ sock_connect_ex(PySocketSockObject *s, PyObject *addro) } res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 0); - if (res < 0) + if (res == -1 && PyErr_Occurred()) { return NULL; + } return PyLong_FromLong((long) res); } From ae72c2eed84cb1fc59791cae0f12b7a05f66f7dd Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 6 Sep 2024 10:17:55 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-09-06-10-17-54.gh-issue-84808.ION67Z.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-09-06-10-17-54.gh-issue-84808.ION67Z.rst diff --git a/Misc/NEWS.d/next/Library/2024-09-06-10-17-54.gh-issue-84808.ION67Z.rst b/Misc/NEWS.d/next/Library/2024-09-06-10-17-54.gh-issue-84808.ION67Z.rst new file mode 100644 index 00000000000000..ffa740daffdc3c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-09-06-10-17-54.gh-issue-84808.ION67Z.rst @@ -0,0 +1 @@ +socketmodule: Handle errors returned by internal_connect() From 2913db31cf3b397bf4514df75940c804258847d4 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 9 Sep 2024 16:32:25 +0200 Subject: [PATCH 3/3] Update NEWS entry --- .../Library/2024-09-06-10-17-54.gh-issue-84808.ION67Z.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-09-06-10-17-54.gh-issue-84808.ION67Z.rst b/Misc/NEWS.d/next/Library/2024-09-06-10-17-54.gh-issue-84808.ION67Z.rst index ffa740daffdc3c..c804c5974241bf 100644 --- a/Misc/NEWS.d/next/Library/2024-09-06-10-17-54.gh-issue-84808.ION67Z.rst +++ b/Misc/NEWS.d/next/Library/2024-09-06-10-17-54.gh-issue-84808.ION67Z.rst @@ -1 +1,3 @@ -socketmodule: Handle errors returned by internal_connect() +Fix error handling in :py:class:`~socket.socket` method +:py:func:`~socket.socket.connect_ex` on platforms where +:c:data:`errno` can be negative.