From cc73932125c9e1d6cada036d90873821ddda091a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 23 Mar 2016 21:35:29 +0100 Subject: [PATCH] socketmodule.c: error if option larger than INT_MAX On Windows, socket.setsockopt() raises an OverflowError if the socket option is larger than INT_MAX bytes. --- Modules/socketmodule.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index ba3cefd177a..8df735d2044 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -2458,13 +2458,26 @@ sock_setsockopt(PySocketSockObject *s, PyObject *args) if (!PyArg_ParseTuple(args, "iiy*:setsockopt", &level, &optname, &optval)) return NULL; +#ifdef MS_WINDOWS + if (optval.len > INT_MAX) { + PyBuffer_Release(&optval); + PyErr_Format(PyExc_OverflowError, + "socket option is larger than %i bytes", + INT_MAX); + return NULL; + } + res = setsockopt(s->sock_fd, level, optname, + optval.buf, (int)optval.len); +#else res = setsockopt(s->sock_fd, level, optname, optval.buf, optval.len); +#endif PyBuffer_Release(&optval); } - if (res < 0) + if (res < 0) { return s->errorhandler(); - Py_INCREF(Py_None); - return Py_None; + } + + Py_RETURN_NONE; } PyDoc_STRVAR(setsockopt_doc,