Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2621a32

Browse files
korliencukou
andcommitted
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 <[email protected]>
1 parent d9efa45 commit 2621a32

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

Modules/socketmodule.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,6 +3405,18 @@ sock_connect_impl(PySocketSockObject *s, void* Py_UNUSED(data))
34053405
return 1;
34063406
}
34073407

3408+
/* Common functionality for socket.connect and socket.connect_ex.
3409+
*
3410+
* If *raise* is set:
3411+
* - On success, return 0.
3412+
* - On any failure, return -1 with an exception set.
3413+
* If *raise* is zero:
3414+
* - On success, return 0.
3415+
* - On connect() failure, return errno (without an exception set)
3416+
* - On other error, return -1 with an exception set.
3417+
*
3418+
* Note that -1 is a valid errno value on some systems.
3419+
*/
34083420
static int
34093421
internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
34103422
int raise)
@@ -3489,8 +3501,10 @@ sock_connect(PySocketSockObject *s, PyObject *addro)
34893501
}
34903502

34913503
res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 1);
3492-
if (res < 0)
3504+
if (res < 0) {
3505+
assert(PyErr_Occurred());
34933506
return NULL;
3507+
}
34943508

34953509
Py_RETURN_NONE;
34963510
}
@@ -3520,8 +3534,9 @@ sock_connect_ex(PySocketSockObject *s, PyObject *addro)
35203534
}
35213535

35223536
res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 0);
3523-
if (res < 0)
3537+
if (res == -1 && PyErr_Occurred()) {
35243538
return NULL;
3539+
}
35253540

35263541
return PyLong_FromLong((long) res);
35273542
}

0 commit comments

Comments
 (0)