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

Skip to content

Commit 524714e

Browse files
committed
socket: use INVALID_SOCKET
* Replace "fd = -1" with "fd = INVALID_SOCKET" * Replace "fd < 0" with "fd == INVALID_SOCKET": SOCKET_T is unsigned on Windows Bug found by Pavel Belikov ("Fragment N1"): http://www.viva64.com/en/b/0414/#ID0ECDAE
1 parent 0cec877 commit 524714e

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

Modules/_ssl.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ struct py_ssl_library_code {
113113
# define HAVE_ALPN
114114
#endif
115115

116+
#ifndef INVALID_SOCKET /* MS defines this */
117+
#define INVALID_SOCKET (-1)
118+
#endif
119+
116120
enum py_ssl_error {
117121
/* these mirror ssl.h */
118122
PY_SSL_ERROR_NONE,
@@ -1699,7 +1703,7 @@ PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout)
16991703
}
17001704

17011705
/* Guard against closed socket */
1702-
if (s->sock_fd < 0)
1706+
if (s->sock_fd == INVALID_SOCKET)
17031707
return SOCKET_HAS_BEEN_CLOSED;
17041708

17051709
/* Prefer poll, if available, since you can poll() any fd
@@ -2023,7 +2027,7 @@ _ssl__SSLSocket_shutdown_impl(PySSLSocket *self)
20232027

20242028
if (sock != NULL) {
20252029
/* Guard against closed socket */
2026-
if ((((PyObject*)sock) == Py_None) || (sock->sock_fd < 0)) {
2030+
if ((((PyObject*)sock) == Py_None) || (sock->sock_fd == INVALID_SOCKET)) {
20272031
_setSSLError("Underlying socket connection gone",
20282032
PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
20292033
return NULL;

Modules/socketmodule.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,8 +2568,9 @@ sock_close(PySocketSockObject *s)
25682568
* and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
25692569
* for more details.
25702570
*/
2571-
if ((fd = s->sock_fd) != -1) {
2572-
s->sock_fd = -1;
2571+
fd = s->sock_fd;
2572+
if (fd != INVALID_SOCKET) {
2573+
s->sock_fd = INVALID_SOCKET;
25732574
Py_BEGIN_ALLOW_THREADS
25742575
(void) SOCKETCLOSE(fd);
25752576
Py_END_ALLOW_THREADS
@@ -2587,7 +2588,7 @@ static PyObject *
25872588
sock_detach(PySocketSockObject *s)
25882589
{
25892590
SOCKET_T fd = s->sock_fd;
2590-
s->sock_fd = -1;
2591+
s->sock_fd = INVALID_SOCKET;
25912592
return PyLong_FromSocket_t(fd);
25922593
}
25932594

@@ -4165,7 +4166,7 @@ static PyGetSetDef sock_getsetlist[] = {
41654166
static void
41664167
sock_dealloc(PySocketSockObject *s)
41674168
{
4168-
if (s->sock_fd != -1) {
4169+
if (s->sock_fd != INVALID_SOCKET) {
41694170
PyObject *exc, *val, *tb;
41704171
Py_ssize_t old_refcount = Py_REFCNT(s);
41714172
++Py_REFCNT(s);
@@ -4221,7 +4222,7 @@ sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
42214222

42224223
new = type->tp_alloc(type, 0);
42234224
if (new != NULL) {
4224-
((PySocketSockObject *)new)->sock_fd = -1;
4225+
((PySocketSockObject *)new)->sock_fd = INVALID_SOCKET;
42254226
((PySocketSockObject *)new)->sock_timeout = _PyTime_FromSeconds(-1);
42264227
((PySocketSockObject *)new)->errorhandler = &set_error;
42274228
}

0 commit comments

Comments
 (0)