Commit 65407fb7 authored by Neal Norwitz's avatar Neal Norwitz

Backport 54594:

Fix SF #1688393, sock.recvfrom(-24) crashes

Also fix some method names that were copied incorrectly (trunk fixed).
parent ef9e09e7
...@@ -583,6 +583,13 @@ class BasicUDPTest(ThreadedUDPSocketTest): ...@@ -583,6 +583,13 @@ class BasicUDPTest(ThreadedUDPSocketTest):
def _testRecvFrom(self): def _testRecvFrom(self):
self.cli.sendto(MSG, 0, (HOST, PORT)) self.cli.sendto(MSG, 0, (HOST, PORT))
def testRecvFromNegative(self):
# Negative lengths passed to recvfrom should give ValueError.
self.assertRaises(ValueError, self.serv.recvfrom, -1)
def _testRecvFromNegative(self):
self.cli.sendto(MSG, 0, (HOST, PORT))
class TCPCloserTest(ThreadedTCPSocketTest): class TCPCloserTest(ThreadedTCPSocketTest):
def testClose(self): def testClose(self):
......
...@@ -134,6 +134,8 @@ Core and builtins ...@@ -134,6 +134,8 @@ Core and builtins
Extension Modules Extension Modules
----------------- -----------------
- Bug #1688393: Prevent crash in socket.recvfrom if length is negative.
- Bug #1622896: fix a rare corner case where the bz2 module raised an - Bug #1622896: fix a rare corner case where the bz2 module raised an
error in spite of a succesful compression. error in spite of a succesful compression.
......
...@@ -2356,14 +2356,14 @@ sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) ...@@ -2356,14 +2356,14 @@ sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
int buflen; int buflen;
/* Get the buffer's memory */ /* Get the buffer's memory */
if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recv", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recv_into", kwlist,
&buf, &buflen, &recvlen, &flags)) &buf, &buflen, &recvlen, &flags))
return NULL; return NULL;
assert(buf != 0 && buflen > 0); assert(buf != 0 && buflen > 0);
if (recvlen < 0) { if (recvlen < 0) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"negative buffersize in recv"); "negative buffersize in recv_into");
return NULL; return NULL;
} }
if (recvlen == 0) { if (recvlen == 0) {
...@@ -2479,6 +2479,12 @@ sock_recvfrom(PySocketSockObject *s, PyObject *args) ...@@ -2479,6 +2479,12 @@ sock_recvfrom(PySocketSockObject *s, PyObject *args)
if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags)) if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))
return NULL; return NULL;
if (recvlen < 0) {
PyErr_SetString(PyExc_ValueError,
"negative buffersize in recvfrom");
return NULL;
}
buf = PyString_FromStringAndSize((char *) 0, recvlen); buf = PyString_FromStringAndSize((char *) 0, recvlen);
if (buf == NULL) if (buf == NULL)
return NULL; return NULL;
...@@ -2525,14 +2531,15 @@ sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) ...@@ -2525,14 +2531,15 @@ sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
PyObject *addr = NULL; PyObject *addr = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recvfrom", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recvfrom_into",
&buf, &buflen, &recvlen, &flags)) kwlist, &buf, &buflen,
&recvlen, &flags))
return NULL; return NULL;
assert(buf != 0 && buflen > 0); assert(buf != 0 && buflen > 0);
if (recvlen < 0) { if (recvlen < 0) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"negative buffersize in recv"); "negative buffersize in recvfrom_into");
return NULL; return NULL;
} }
if (recvlen == 0) { if (recvlen == 0) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment