diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -590,6 +590,7 @@ static int internal_setblocking(PySocketSockObject *s, int block) { + int have_error = 0; #ifndef MS_WINDOWS int delay_flag; #endif @@ -604,23 +605,38 @@ #ifndef MS_WINDOWS #if defined(__VMS) block = !block; - ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); + if (ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block) == -1) { + have_error = 1; + } #else /* !__VMS */ delay_flag = fcntl(s->sock_fd, F_GETFL, 0); if (block) delay_flag &= (~O_NONBLOCK); else delay_flag |= O_NONBLOCK; - fcntl(s->sock_fd, F_SETFL, delay_flag); + if (fcntl(s->sock_fd, F_SETFL, delay_flag) == -1) { + have_error = 1; + } #endif /* !__VMS */ #else /* MS_WINDOWS */ block = !block; - ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); + if (ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block) != NO_ERROR) { + have_error = 1; + } #endif /* MS_WINDOWS */ Py_END_ALLOW_THREADS + if (have_error) { +#ifndef MS_WINDOWS + PyErr_SetFromErrno(PyExc_OSError); +#else + PyErr_SetFromWindowsErr(0); +#endif + return -1; + } + /* Since these don't return anything */ - return 1; + return 0; } /* Do a select()/poll() on the socket, if necessary (sock_timeout > 0). @@ -737,7 +753,7 @@ static double defaulttimeout = -1.0; /* Default timeout for new sockets */ -static void +static int init_sockobject(PySocketSockObject *s, SOCKET_T fd, int family, int type, int proto) { @@ -755,9 +771,9 @@ { s->sock_timeout = defaulttimeout; if (defaulttimeout >= 0.0) - internal_setblocking(s, 0); - } - + return internal_setblocking(s, 0); + } + return 0; } @@ -772,8 +788,12 @@ PySocketSockObject *s; s = (PySocketSockObject *) PyType_GenericNew(&sock_type, NULL, NULL); - if (s != NULL) - init_sockobject(s, fd, family, type, proto); + if (s != NULL) { + if (init_sockobject(s, fd, family, type, proto) == -1) { + Py_DECREF(s); + return NULL; + } + } return s; } @@ -2019,8 +2039,10 @@ if (block == -1 && PyErr_Occurred()) return NULL; + if (internal_setblocking(s, block) == -1) { + return NULL; + } s->sock_timeout = block ? -1.0 : 0.0; - internal_setblocking(s, block); Py_INCREF(Py_None); return Py_None; @@ -2056,8 +2078,10 @@ } } + if (internal_setblocking(s, timeout < 0.0) == -1) { + return NULL; + } s->sock_timeout = timeout; - internal_setblocking(s, timeout < 0.0); Py_INCREF(Py_None); return Py_None; @@ -3971,7 +3995,9 @@ return -1; } } - init_sockobject(s, fd, family, type, proto); + if (init_sockobject(s, fd, family, type, proto) == -1) { + return -1; + } return 0;