[Python-checkins] cpython: socketmodule.c: error if option larger than INT_MAX

victor.stinner python-checkins at python.org
Wed Mar 23 16:38:09 EDT 2016


https://hg.python.org/cpython/rev/ea855340c11e
changeset:   100708:ea855340c11e
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Mar 23 21:35:29 2016 +0100
summary:
  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.

files:
  Modules/socketmodule.c |  19 ++++++++++++++++---
  1 files changed, 16 insertions(+), 3 deletions(-)


diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -2458,13 +2458,26 @@
         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,

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list