From 41a00848ffc20c47094111511ea37b9f176eb0af Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Thu, 17 Aug 2017 14:53:06 +0300 Subject: [PATCH 1/7] passes testBugsOrPatches.py patches tests. --- Modules/_ctypes/_ctypes.c | 3 ++- Modules/_io/textio.c | 5 ++++- Modules/audioop.c | 15 ++++++++++----- Modules/itertoolsmodule.c | 8 +++++++- Modules/overlapped.c | 9 ++++++++- Modules/socketmodule.c | 8 +++++--- Modules/timemodule.c | 16 +++++++++++----- Objects/exceptions.c | 7 +++++++ Python/bltinmodule.c | 16 +++++++++++++++- 9 files changed, 69 insertions(+), 18 deletions(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index fe39567374d0383..dee1c4b8fdb115d 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -3262,7 +3262,8 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds) /* Here ftuple is a borrowed reference */ return NULL; - if (!PyArg_ParseTuple(ftuple, "O&O", _get_name, &name, &dll)) { + if (!PyArg_ParseTuple(ftuple, "O&O;illegal func_spec argument", + _get_name, &name, &dll)) { Py_DECREF(ftuple); return NULL; } diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index b5d368a175c94cc..4719e3967c45dc3 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -562,8 +562,11 @@ _io_IncrementalNewlineDecoder_setstate(nldecoder_object *self, PyObject *buffer; unsigned long long flag; - if (!PyArg_ParseTuple(state, "OK", &buffer, &flag)) + if (!PyArg_ParseTuple(state, + "OK;IncrementalNewlineDecoder.setstate(): illegal " + "state argument", &buffer, &flag)) { return NULL; + } self->pendingcr = (int) (flag & 1); flag >>= 1; diff --git a/Modules/audioop.c b/Modules/audioop.c index dcd7788d6218620..bc78852a64d0b12 100644 --- a/Modules/audioop.c +++ b/Modules/audioop.c @@ -1355,7 +1355,7 @@ audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width, } else { if (!PyArg_ParseTuple(state, - "iO!;audioop.ratecv: illegal state argument", + "iO!;ratecv(): illegal state argument", &d, &PyTuple_Type, &samps)) goto exit; if (PyTuple_Size(samps) != nchannels) { @@ -1365,9 +1365,10 @@ audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width, } for (chan = 0; chan < nchannels; chan++) { if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan), - "ii:ratecv", &prev_i[chan], - &cur_i[chan])) + "ii;ratecv(): illegal state argument", + &prev_i[chan], &cur_i[chan])) { goto exit; + } } } @@ -1638,7 +1639,9 @@ audioop_lin2adpcm_impl(PyObject *module, Py_buffer *fragment, int width, PyErr_SetString(PyExc_TypeError, "state must be a tuple or None"); return NULL; } - else if (!PyArg_ParseTuple(state, "ii", &valpred, &index)) { + else if (!PyArg_ParseTuple(state, + "ii;lin2adpcm(): illegal state argument", + &valpred, &index)) { return NULL; } else if (valpred >= 0x8000 || valpred < -0x8000 || @@ -1766,7 +1769,9 @@ audioop_adpcm2lin_impl(PyObject *module, Py_buffer *fragment, int width, PyErr_SetString(PyExc_TypeError, "state must be a tuple or None"); return NULL; } - else if (!PyArg_ParseTuple(state, "ii", &valpred, &index)) { + else if (!PyArg_ParseTuple(state, + "ii;adpcm2lin(): illegal state argument", + &valpred, &index)) { return NULL; } else if (valpred >= 0x8000 || valpred < -0x8000 || diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index d7a1ef074280581..de9f93028de54ae 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -2027,7 +2027,7 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_ssize_t nargs, npools, repeat=1; PyObject *pools = NULL; Py_ssize_t *indices = NULL; - Py_ssize_t i; + Py_ssize_t i, num_kwds; if (kwds != NULL) { char *kwlist[] = {"repeat", 0}; @@ -2036,6 +2036,12 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return NULL; if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", kwlist, &repeat)) { + num_kwds = PyDict_GET_SIZE(kwds); + if (num_kwds > 1) { + PyErr_Format(PyExc_TypeError, + "product() takes at most 1 keyword argument " + "(%zd given)", num_kwds); + } Py_DECREF(tmpargs); return NULL; } diff --git a/Modules/overlapped.c b/Modules/overlapped.c index 0aa8657898da13e..6daa660d45852f1 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -980,6 +980,11 @@ parse_address(PyObject *obj, SOCKADDR *Address, int Length) memset(Address, 0, Length); + if (!PyTuple_CheckExact(obj)) { + PyErr_SetString(PyExc_TypeError, "ConnectEx(): bytes_as_address " + "argument must be a tuple"); + return -1; + } if (PyArg_ParseTuple(obj, "uH", &Host, &Port)) { Address->sa_family = AF_INET; @@ -990,7 +995,9 @@ parse_address(PyObject *obj, SOCKADDR *Address, int Length) ((SOCKADDR_IN*)Address)->sin_port = htons(Port); return Length; } - else if (PyArg_ParseTuple(obj, "uHkk", &Host, &Port, &FlowInfo, &ScopeId)) + else if (PyArg_ParseTuple(obj, + "uHkk;ConnectEx(): illegal address_as_bytes " + "argument", &Host, &Port, &FlowInfo, &ScopeId)) { PyErr_Clear(); Address->sa_family = AF_INET6; diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index e18dd32d90045d8..c03d66c62b2189a 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -5972,12 +5972,14 @@ socket_getnameinfo(PyObject *self, PyObject *args) "getnameinfo() argument 1 must be a tuple"); return NULL; } - if (!PyArg_ParseTuple(sa, "si|II", - &hostp, &port, &flowinfo, &scope_id)) + if (!PyArg_ParseTuple(sa, + "si|II;getnameinfo(): illegal sockaddr argument", + &hostp, &port, &flowinfo, &scope_id)) { return NULL; + } if (flowinfo > 0xfffff) { PyErr_SetString(PyExc_OverflowError, - "getsockaddrarg: flowinfo must be 0-1048575."); + "getnameinfo(): flowinfo must be 0-1048575."); return NULL; } PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 15c467b9b4074eb..8d8bddfeff477be 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -415,7 +415,7 @@ When 'seconds' is not passed in, convert the current time instead."); * an exception and return 0 on error. */ static int -gettmarg(PyObject *args, struct tm *p) +gettmarg(PyObject *args, struct tm *p, const char *format) { int y; @@ -427,7 +427,7 @@ gettmarg(PyObject *args, struct tm *p) return 0; } - if (!PyArg_ParseTuple(args, "iiiiiiiii", + if (!PyArg_ParseTuple(args, format, &y, &p->tm_mon, &p->tm_mday, &p->tm_hour, &p->tm_min, &p->tm_sec, &p->tm_wday, &p->tm_yday, &p->tm_isdst)) @@ -586,8 +586,10 @@ time_strftime(PyObject *self, PyObject *args) if (_PyTime_localtime(tt, &buf) != 0) return NULL; } - else if (!gettmarg(tup, &buf) || !checktm(&buf)) + else if (!gettmarg(tup, &buf, "iiiiiiiii;strftime(): illegal time tuple " + "argument") || !checktm(&buf)) { return NULL; + } #if defined(_MSC_VER) || defined(sun) || defined(_AIX) if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) { @@ -777,8 +779,10 @@ time_asctime(PyObject *self, PyObject *args) if (_PyTime_localtime(tt, &buf) != 0) return NULL; - } else if (!gettmarg(tup, &buf) || !checktm(&buf)) + } else if (!gettmarg(tup, &buf, "iiiiiiiii;asctime(): illegal time tuple " + "argument") || !checktm(&buf)) { return NULL; + } return _asctime(&buf); } @@ -814,8 +818,10 @@ time_mktime(PyObject *self, PyObject *tup) { struct tm buf; time_t tt; - if (!gettmarg(tup, &buf)) + if (!gettmarg(tup, &buf, "iiiiiiiii;mktime(): illegal time tuple " + "argument")) { return NULL; + } #ifdef _AIX /* year < 1902 or year > 2037 */ if (buf.tm_year < 2 || buf.tm_year > 137) { diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 190ad0654024b09..1c784641a3a69f5 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -615,6 +615,7 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds) PyObject *msg = NULL; PyObject *name = NULL; PyObject *path = NULL; + Py_ssize_t num_kwds; if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1) return -1; @@ -624,6 +625,12 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds) return -1; if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OO:ImportError", kwlist, &name, &path)) { + num_kwds = PyDict_GET_SIZE(kwds); + if (num_kwds > 2) { + PyErr_Format(PyExc_TypeError, + "ImportError() takes at most 2 keyword arguments " + "(%zd given)", num_kwds); + } Py_DECREF(empty_tuple); return -1; } diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 85f207b68ee3590..a0ab5be00790a03 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1510,6 +1510,7 @@ min_max(PyObject *args, PyObject *kwds, int op) const char *name = op == Py_LT ? "min" : "max"; const int positional = PyTuple_Size(args) > 1; int ret; + Py_ssize_t num_kwds; if (positional) v = args; @@ -1522,8 +1523,15 @@ min_max(PyObject *args, PyObject *kwds, int op) ret = PyArg_ParseTupleAndKeywords(emptytuple, kwds, "|$OO", kwlist, &keyfunc, &defaultval); Py_DECREF(emptytuple); - if (!ret) + if (!ret) { + num_kwds = PyDict_GET_SIZE(kwds); + if (num_kwds > 2) { + PyErr_Format(PyExc_TypeError, + "%s() takes at most 2 keyword arguments (%zd given)", + name, num_kwds); + } return NULL; + } if (positional && defaultval != NULL) { PyErr_Format(PyExc_TypeError, @@ -1741,10 +1749,16 @@ builtin_print(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnam static struct _PyArg_Parser _parser = {"|OOOO:print", _keywords, 0}; PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL; int i, err; + Py_ssize_t num_kwnames; if (kwnames != NULL && !_PyArg_ParseStackAndKeywords(args + nargs, 0, kwnames, &_parser, &sep, &end, &file, &flush)) { + num_kwnames = PyTuple_GET_SIZE(kwnames); + if (num_kwnames > 4) { + PyErr_Format(PyExc_TypeError, "print() takes at most 4 keyword " + "arguments (%zd given)", num_kwnames); + } return NULL; } From 8d19d624461e8c99b7549289a79d9e390153cd7f Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Thu, 17 Aug 2017 16:39:37 +0300 Subject: [PATCH 2/7] changed to using O\!, as Serhiy advised --- Modules/overlapped.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Modules/overlapped.c b/Modules/overlapped.c index 6daa660d45852f1..2276a83278f3f11 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -980,11 +980,6 @@ parse_address(PyObject *obj, SOCKADDR *Address, int Length) memset(Address, 0, Length); - if (!PyTuple_CheckExact(obj)) { - PyErr_SetString(PyExc_TypeError, "ConnectEx(): bytes_as_address " - "argument must be a tuple"); - return -1; - } if (PyArg_ParseTuple(obj, "uH", &Host, &Port)) { Address->sa_family = AF_INET; @@ -1031,7 +1026,8 @@ Overlapped_ConnectEx(OverlappedObject *self, PyObject *args) BOOL ret; DWORD err; - if (!PyArg_ParseTuple(args, F_HANDLE "O", &ConnectSocket, &AddressObj)) + if (!PyArg_ParseTuple(args, F_HANDLE "O!", &ConnectSocket, + &PyTuple_Type, &AddressObj)) return NULL; if (self->type != TYPE_NONE) { From be417971d708f5363a21492aa5f78b2a9b13413f Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Thu, 17 Aug 2017 17:04:32 +0300 Subject: [PATCH 3/7] removed fixes of the too-many-keyword-arguments-wrong-err-msgs issue --- Modules/itertoolsmodule.c | 8 +------- Modules/overlapped.c | 2 +- Objects/exceptions.c | 7 ------- Python/bltinmodule.c | 16 +--------------- 4 files changed, 3 insertions(+), 30 deletions(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index de9f93028de54ae..d7a1ef074280581 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -2027,7 +2027,7 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_ssize_t nargs, npools, repeat=1; PyObject *pools = NULL; Py_ssize_t *indices = NULL; - Py_ssize_t i, num_kwds; + Py_ssize_t i; if (kwds != NULL) { char *kwlist[] = {"repeat", 0}; @@ -2036,12 +2036,6 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return NULL; if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", kwlist, &repeat)) { - num_kwds = PyDict_GET_SIZE(kwds); - if (num_kwds > 1) { - PyErr_Format(PyExc_TypeError, - "product() takes at most 1 keyword argument " - "(%zd given)", num_kwds); - } Py_DECREF(tmpargs); return NULL; } diff --git a/Modules/overlapped.c b/Modules/overlapped.c index 2276a83278f3f11..efab78f6164aeaa 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -1026,7 +1026,7 @@ Overlapped_ConnectEx(OverlappedObject *self, PyObject *args) BOOL ret; DWORD err; - if (!PyArg_ParseTuple(args, F_HANDLE "O!", &ConnectSocket, + if (!PyArg_ParseTuple(args, F_HANDLE "O!:ConnectEx", &ConnectSocket, &PyTuple_Type, &AddressObj)) return NULL; diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 1c784641a3a69f5..190ad0654024b09 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -615,7 +615,6 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds) PyObject *msg = NULL; PyObject *name = NULL; PyObject *path = NULL; - Py_ssize_t num_kwds; if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1) return -1; @@ -625,12 +624,6 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds) return -1; if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OO:ImportError", kwlist, &name, &path)) { - num_kwds = PyDict_GET_SIZE(kwds); - if (num_kwds > 2) { - PyErr_Format(PyExc_TypeError, - "ImportError() takes at most 2 keyword arguments " - "(%zd given)", num_kwds); - } Py_DECREF(empty_tuple); return -1; } diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index a0ab5be00790a03..85f207b68ee3590 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1510,7 +1510,6 @@ min_max(PyObject *args, PyObject *kwds, int op) const char *name = op == Py_LT ? "min" : "max"; const int positional = PyTuple_Size(args) > 1; int ret; - Py_ssize_t num_kwds; if (positional) v = args; @@ -1523,15 +1522,8 @@ min_max(PyObject *args, PyObject *kwds, int op) ret = PyArg_ParseTupleAndKeywords(emptytuple, kwds, "|$OO", kwlist, &keyfunc, &defaultval); Py_DECREF(emptytuple); - if (!ret) { - num_kwds = PyDict_GET_SIZE(kwds); - if (num_kwds > 2) { - PyErr_Format(PyExc_TypeError, - "%s() takes at most 2 keyword arguments (%zd given)", - name, num_kwds); - } + if (!ret) return NULL; - } if (positional && defaultval != NULL) { PyErr_Format(PyExc_TypeError, @@ -1749,16 +1741,10 @@ builtin_print(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnam static struct _PyArg_Parser _parser = {"|OOOO:print", _keywords, 0}; PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL; int i, err; - Py_ssize_t num_kwnames; if (kwnames != NULL && !_PyArg_ParseStackAndKeywords(args + nargs, 0, kwnames, &_parser, &sep, &end, &file, &flush)) { - num_kwnames = PyTuple_GET_SIZE(kwnames); - if (num_kwnames > 4) { - PyErr_Format(PyExc_TypeError, "print() takes at most 4 keyword " - "arguments (%zd given)", num_kwnames); - } return NULL; } From f1446c1d1134b69d752ef7f0ed83223cf1a7b146 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Thu, 17 Aug 2017 17:21:38 +0300 Subject: [PATCH 4/7] added some curly braces --- Modules/overlapped.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/overlapped.c b/Modules/overlapped.c index efab78f6164aeaa..3d4e37986ad00e6 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -1027,8 +1027,9 @@ Overlapped_ConnectEx(OverlappedObject *self, PyObject *args) DWORD err; if (!PyArg_ParseTuple(args, F_HANDLE "O!:ConnectEx", &ConnectSocket, - &PyTuple_Type, &AddressObj)) + &PyTuple_Type, &AddressObj)) { return NULL; + } if (self->type != TYPE_NONE) { PyErr_SetString(PyExc_ValueError, "operation already attempted"); From 978a5d6f2d6966de72958cfd95fa5fa21a115ca2 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 18 Aug 2017 16:31:48 +0300 Subject: [PATCH 5/7] fix braces according to PEP 7, and add checks whether objects are tuples. --- Lib/test/test_audioop.py | 4 ++++ Lib/test/test_io.py | 1 + Modules/_ctypes/_ctypes.c | 3 ++- Modules/_io/textio.c | 10 +++++++--- Modules/audioop.c | 29 ++++++++++++++++++++--------- Modules/overlapped.c | 3 ++- Modules/socketmodule.c | 6 +++--- Modules/timemodule.c | 9 ++++++--- 8 files changed, 45 insertions(+), 20 deletions(-) diff --git a/Lib/test/test_audioop.py b/Lib/test/test_audioop.py index 8f34d72427e1833..9baa62ad45c0453 100644 --- a/Lib/test/test_audioop.py +++ b/Lib/test/test_audioop.py @@ -405,6 +405,10 @@ def test_ratecv(self): self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 30, 10)[0], expected[w]) + self.assertRaises(TypeError, audioop.ratecv, b'', 1, 1, 8000, 8000, 42) + self.assertRaises(TypeError, audioop.ratecv, + b'', 1, 1, 8000, 8000, (1, (42,))) + def test_reverse(self): for w in 1, 2, 3, 4: self.assertEqual(audioop.reverse(b'', w), b'') diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 83d6c4e57ecbeed..ab0cbe163613ecd 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -3472,6 +3472,7 @@ def test_newline_decoder(self): decoder = codecs.getincrementaldecoder("utf-8")() decoder = self.IncrementalNewlineDecoder(decoder, translate=True) self.check_newline_decoding_utf8(decoder) + self.assertRaises(TypeError, decoder.setstate, 42) def test_newline_bytes(self): # Issue 5433: Excessive optimization in IncrementalNewlineDecoder diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index dee1c4b8fdb115d..010658ebcd481f1 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -3263,7 +3263,8 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds) return NULL; if (!PyArg_ParseTuple(ftuple, "O&O;illegal func_spec argument", - _get_name, &name, &dll)) { + _get_name, &name, &dll)) + { Py_DECREF(ftuple); return NULL; } diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 4719e3967c45dc3..57b66d66c337452 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -562,9 +562,13 @@ _io_IncrementalNewlineDecoder_setstate(nldecoder_object *self, PyObject *buffer; unsigned long long flag; - if (!PyArg_ParseTuple(state, - "OK;IncrementalNewlineDecoder.setstate(): illegal " - "state argument", &buffer, &flag)) { + if (!PyTuple_Check(state)) { + PyErr_SetString(PyExc_TypeError, "state argument must be a tuple"); + return NULL; + } + if (!PyArg_ParseTuple(state, "OK;setstate(): illegal state argument", + &buffer, &flag)) + { return NULL; } diff --git a/Modules/audioop.c b/Modules/audioop.c index bc78852a64d0b12..ad2c8854e221367 100644 --- a/Modules/audioop.c +++ b/Modules/audioop.c @@ -1293,7 +1293,7 @@ audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width, char *cp, *ncp; Py_ssize_t len; int chan, d, *prev_i, *cur_i, cur_o; - PyObject *samps, *str, *rv = NULL; + PyObject *samps, *str, *rv = NULL, *channel; int bytes_per_frame; if (!audioop_check_size(width)) @@ -1354,6 +1354,10 @@ audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width, prev_i[chan] = cur_i[chan] = 0; } else { + if (!PyTuple_Check(state)) { + PyErr_SetString(PyExc_TypeError, "state must be a tuple or None"); + goto exit; + } if (!PyArg_ParseTuple(state, "iO!;ratecv(): illegal state argument", &d, &PyTuple_Type, &samps)) @@ -1364,9 +1368,16 @@ audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width, goto exit; } for (chan = 0; chan < nchannels; chan++) { - if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan), + channel = PyTuple_GetItem(samps, chan); + if (!PyTuple_Check(channel)) { + PyErr_SetString(PyExc_TypeError, + "ratecv(): channel must be a tuple"); + goto exit; + } + if (!PyArg_ParseTuple(channel, "ii;ratecv(): illegal state argument", - &prev_i[chan], &cur_i[chan])) { + &prev_i[chan], &cur_i[chan])) + { goto exit; } } @@ -1639,9 +1650,9 @@ audioop_lin2adpcm_impl(PyObject *module, Py_buffer *fragment, int width, PyErr_SetString(PyExc_TypeError, "state must be a tuple or None"); return NULL; } - else if (!PyArg_ParseTuple(state, - "ii;lin2adpcm(): illegal state argument", - &valpred, &index)) { + else if (!PyArg_ParseTuple(state, "ii;lin2adpcm(): illegal state argument", + &valpred, &index)) + { return NULL; } else if (valpred >= 0x8000 || valpred < -0x8000 || @@ -1769,9 +1780,9 @@ audioop_adpcm2lin_impl(PyObject *module, Py_buffer *fragment, int width, PyErr_SetString(PyExc_TypeError, "state must be a tuple or None"); return NULL; } - else if (!PyArg_ParseTuple(state, - "ii;adpcm2lin(): illegal state argument", - &valpred, &index)) { + else if (!PyArg_ParseTuple(state, "ii;adpcm2lin(): illegal state argument", + &valpred, &index)) + { return NULL; } else if (valpred >= 0x8000 || valpred < -0x8000 || diff --git a/Modules/overlapped.c b/Modules/overlapped.c index 3d4e37986ad00e6..fe3287a7d89d759 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -1027,7 +1027,8 @@ Overlapped_ConnectEx(OverlappedObject *self, PyObject *args) DWORD err; if (!PyArg_ParseTuple(args, F_HANDLE "O!:ConnectEx", &ConnectSocket, - &PyTuple_Type, &AddressObj)) { + &PyTuple_Type, &AddressObj)) + { return NULL; } diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index c03d66c62b2189a..bf8d19fe2f40cb6 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -5972,9 +5972,9 @@ socket_getnameinfo(PyObject *self, PyObject *args) "getnameinfo() argument 1 must be a tuple"); return NULL; } - if (!PyArg_ParseTuple(sa, - "si|II;getnameinfo(): illegal sockaddr argument", - &hostp, &port, &flowinfo, &scope_id)) { + if (!PyArg_ParseTuple(sa, "si|II;getnameinfo(): illegal sockaddr argument", + &hostp, &port, &flowinfo, &scope_id)) + { return NULL; } if (flowinfo > 0xfffff) { diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 8d8bddfeff477be..1b6b0b8349d0a07 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -587,7 +587,8 @@ time_strftime(PyObject *self, PyObject *args) return NULL; } else if (!gettmarg(tup, &buf, "iiiiiiiii;strftime(): illegal time tuple " - "argument") || !checktm(&buf)) { + "argument") || !checktm(&buf)) + { return NULL; } @@ -780,7 +781,8 @@ time_asctime(PyObject *self, PyObject *args) return NULL; } else if (!gettmarg(tup, &buf, "iiiiiiiii;asctime(): illegal time tuple " - "argument") || !checktm(&buf)) { + "argument") || !checktm(&buf)) + { return NULL; } return _asctime(&buf); @@ -819,7 +821,8 @@ time_mktime(PyObject *self, PyObject *tup) struct tm buf; time_t tt; if (!gettmarg(tup, &buf, "iiiiiiiii;mktime(): illegal time tuple " - "argument")) { + "argument")) + { return NULL; } #ifdef _AIX From d52719e8fcb62478adf41aa5781da1b465192729 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Sat, 19 Aug 2017 09:56:59 +0300 Subject: [PATCH 6/7] dont mention the undocumented 'channel' in error message, and beautify some of the code --- Modules/audioop.c | 2 +- Modules/overlapped.c | 4 ++-- Modules/timemodule.c | 14 ++++++++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Modules/audioop.c b/Modules/audioop.c index ad2c8854e221367..80c8a2a8e5210b7 100644 --- a/Modules/audioop.c +++ b/Modules/audioop.c @@ -1371,7 +1371,7 @@ audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width, channel = PyTuple_GetItem(samps, chan); if (!PyTuple_Check(channel)) { PyErr_SetString(PyExc_TypeError, - "ratecv(): channel must be a tuple"); + "ratecv(): illegal state argument"); goto exit; } if (!PyArg_ParseTuple(channel, diff --git a/Modules/overlapped.c b/Modules/overlapped.c index fe3287a7d89d759..4522e6fa361174b 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -1026,8 +1026,8 @@ Overlapped_ConnectEx(OverlappedObject *self, PyObject *args) BOOL ret; DWORD err; - if (!PyArg_ParseTuple(args, F_HANDLE "O!:ConnectEx", &ConnectSocket, - &PyTuple_Type, &AddressObj)) + if (!PyArg_ParseTuple(args, F_HANDLE "O!:ConnectEx", + &ConnectSocket, &PyTuple_Type, &AddressObj)) { return NULL; } diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 1b6b0b8349d0a07..eef8f0629f8fe02 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -586,8 +586,9 @@ time_strftime(PyObject *self, PyObject *args) if (_PyTime_localtime(tt, &buf) != 0) return NULL; } - else if (!gettmarg(tup, &buf, "iiiiiiiii;strftime(): illegal time tuple " - "argument") || !checktm(&buf)) + else if (!gettmarg(tup, &buf, + "iiiiiiiii;strftime(): illegal time tuple argument") || + !checktm(&buf)) { return NULL; } @@ -780,8 +781,9 @@ time_asctime(PyObject *self, PyObject *args) if (_PyTime_localtime(tt, &buf) != 0) return NULL; - } else if (!gettmarg(tup, &buf, "iiiiiiiii;asctime(): illegal time tuple " - "argument") || !checktm(&buf)) + } else if (!gettmarg(tup, &buf, + "iiiiiiiii;asctime(): illegal time tuple argument") || + !checktm(&buf)) { return NULL; } @@ -820,8 +822,8 @@ time_mktime(PyObject *self, PyObject *tup) { struct tm buf; time_t tt; - if (!gettmarg(tup, &buf, "iiiiiiiii;mktime(): illegal time tuple " - "argument")) + if (!gettmarg(tup, &buf, + "iiiiiiiii;mktime(): illegal time tuple argument")) { return NULL; } From 868e910eca27ef564c864f5ec7c5a56f045c065f Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Sun, 20 Aug 2017 14:52:51 +0300 Subject: [PATCH 7/7] added a line break before else --- Modules/timemodule.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Modules/timemodule.c b/Modules/timemodule.c index eef8f0629f8fe02..36a95bbcedd6fef 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -780,10 +780,10 @@ time_asctime(PyObject *self, PyObject *args) time_t tt = time(NULL); if (_PyTime_localtime(tt, &buf) != 0) return NULL; - - } else if (!gettmarg(tup, &buf, - "iiiiiiiii;asctime(): illegal time tuple argument") || - !checktm(&buf)) + } + else if (!gettmarg(tup, &buf, + "iiiiiiiii;asctime(): illegal time tuple argument") || + !checktm(&buf)) { return NULL; }