Skip to content

Commit 1d1d3e9

Browse files
orenmnserhiy-storchaka
authored andcommitted
bpo-28261: Fixed err msgs where PyArg_ParseTuple is used to parse normal tuples. (#3119)
1 parent 4bfebc6 commit 1d1d3e9

File tree

8 files changed

+67
-19
lines changed

8 files changed

+67
-19
lines changed

Lib/test/test_audioop.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ def test_ratecv(self):
405405
self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 30, 10)[0],
406406
expected[w])
407407

408+
self.assertRaises(TypeError, audioop.ratecv, b'', 1, 1, 8000, 8000, 42)
409+
self.assertRaises(TypeError, audioop.ratecv,
410+
b'', 1, 1, 8000, 8000, (1, (42,)))
411+
408412
def test_reverse(self):
409413
for w in 1, 2, 3, 4:
410414
self.assertEqual(audioop.reverse(b'', w), b'')

Lib/test/test_io.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3472,6 +3472,7 @@ def test_newline_decoder(self):
34723472
decoder = codecs.getincrementaldecoder("utf-8")()
34733473
decoder = self.IncrementalNewlineDecoder(decoder, translate=True)
34743474
self.check_newline_decoding_utf8(decoder)
3475+
self.assertRaises(TypeError, decoder.setstate, 42)
34753476

34763477
def test_newline_bytes(self):
34773478
# Issue 5433: Excessive optimization in IncrementalNewlineDecoder

Modules/_ctypes/_ctypes.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3262,7 +3262,9 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
32623262
/* Here ftuple is a borrowed reference */
32633263
return NULL;
32643264

3265-
if (!PyArg_ParseTuple(ftuple, "O&O", _get_name, &name, &dll)) {
3265+
if (!PyArg_ParseTuple(ftuple, "O&O;illegal func_spec argument",
3266+
_get_name, &name, &dll))
3267+
{
32663268
Py_DECREF(ftuple);
32673269
return NULL;
32683270
}

Modules/_io/textio.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,15 @@ _io_IncrementalNewlineDecoder_setstate(nldecoder_object *self,
562562
PyObject *buffer;
563563
unsigned long long flag;
564564

565-
if (!PyArg_ParseTuple(state, "OK", &buffer, &flag))
565+
if (!PyTuple_Check(state)) {
566+
PyErr_SetString(PyExc_TypeError, "state argument must be a tuple");
566567
return NULL;
568+
}
569+
if (!PyArg_ParseTuple(state, "OK;setstate(): illegal state argument",
570+
&buffer, &flag))
571+
{
572+
return NULL;
573+
}
567574

568575
self->pendingcr = (int) (flag & 1);
569576
flag >>= 1;

Modules/audioop.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width,
12931293
char *cp, *ncp;
12941294
Py_ssize_t len;
12951295
int chan, d, *prev_i, *cur_i, cur_o;
1296-
PyObject *samps, *str, *rv = NULL;
1296+
PyObject *samps, *str, *rv = NULL, *channel;
12971297
int bytes_per_frame;
12981298

12991299
if (!audioop_check_size(width))
@@ -1354,8 +1354,12 @@ audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width,
13541354
prev_i[chan] = cur_i[chan] = 0;
13551355
}
13561356
else {
1357+
if (!PyTuple_Check(state)) {
1358+
PyErr_SetString(PyExc_TypeError, "state must be a tuple or None");
1359+
goto exit;
1360+
}
13571361
if (!PyArg_ParseTuple(state,
1358-
"iO!;audioop.ratecv: illegal state argument",
1362+
"iO!;ratecv(): illegal state argument",
13591363
&d, &PyTuple_Type, &samps))
13601364
goto exit;
13611365
if (PyTuple_Size(samps) != nchannels) {
@@ -1364,10 +1368,18 @@ audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width,
13641368
goto exit;
13651369
}
13661370
for (chan = 0; chan < nchannels; chan++) {
1367-
if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
1368-
"ii:ratecv", &prev_i[chan],
1369-
&cur_i[chan]))
1371+
channel = PyTuple_GetItem(samps, chan);
1372+
if (!PyTuple_Check(channel)) {
1373+
PyErr_SetString(PyExc_TypeError,
1374+
"ratecv(): illegal state argument");
13701375
goto exit;
1376+
}
1377+
if (!PyArg_ParseTuple(channel,
1378+
"ii;ratecv(): illegal state argument",
1379+
&prev_i[chan], &cur_i[chan]))
1380+
{
1381+
goto exit;
1382+
}
13711383
}
13721384
}
13731385

@@ -1638,7 +1650,9 @@ audioop_lin2adpcm_impl(PyObject *module, Py_buffer *fragment, int width,
16381650
PyErr_SetString(PyExc_TypeError, "state must be a tuple or None");
16391651
return NULL;
16401652
}
1641-
else if (!PyArg_ParseTuple(state, "ii", &valpred, &index)) {
1653+
else if (!PyArg_ParseTuple(state, "ii;lin2adpcm(): illegal state argument",
1654+
&valpred, &index))
1655+
{
16421656
return NULL;
16431657
}
16441658
else if (valpred >= 0x8000 || valpred < -0x8000 ||
@@ -1766,7 +1780,9 @@ audioop_adpcm2lin_impl(PyObject *module, Py_buffer *fragment, int width,
17661780
PyErr_SetString(PyExc_TypeError, "state must be a tuple or None");
17671781
return NULL;
17681782
}
1769-
else if (!PyArg_ParseTuple(state, "ii", &valpred, &index)) {
1783+
else if (!PyArg_ParseTuple(state, "ii;adpcm2lin(): illegal state argument",
1784+
&valpred, &index))
1785+
{
17701786
return NULL;
17711787
}
17721788
else if (valpred >= 0x8000 || valpred < -0x8000 ||

Modules/overlapped.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,9 @@ parse_address(PyObject *obj, SOCKADDR *Address, int Length)
990990
((SOCKADDR_IN*)Address)->sin_port = htons(Port);
991991
return Length;
992992
}
993-
else if (PyArg_ParseTuple(obj, "uHkk", &Host, &Port, &FlowInfo, &ScopeId))
993+
else if (PyArg_ParseTuple(obj,
994+
"uHkk;ConnectEx(): illegal address_as_bytes "
995+
"argument", &Host, &Port, &FlowInfo, &ScopeId))
994996
{
995997
PyErr_Clear();
996998
Address->sa_family = AF_INET6;
@@ -1024,8 +1026,11 @@ Overlapped_ConnectEx(OverlappedObject *self, PyObject *args)
10241026
BOOL ret;
10251027
DWORD err;
10261028

1027-
if (!PyArg_ParseTuple(args, F_HANDLE "O", &ConnectSocket, &AddressObj))
1029+
if (!PyArg_ParseTuple(args, F_HANDLE "O!:ConnectEx",
1030+
&ConnectSocket, &PyTuple_Type, &AddressObj))
1031+
{
10281032
return NULL;
1033+
}
10291034

10301035
if (self->type != TYPE_NONE) {
10311036
PyErr_SetString(PyExc_ValueError, "operation already attempted");

Modules/socketmodule.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5972,12 +5972,14 @@ socket_getnameinfo(PyObject *self, PyObject *args)
59725972
"getnameinfo() argument 1 must be a tuple");
59735973
return NULL;
59745974
}
5975-
if (!PyArg_ParseTuple(sa, "si|II",
5975+
if (!PyArg_ParseTuple(sa, "si|II;getnameinfo(): illegal sockaddr argument",
59765976
&hostp, &port, &flowinfo, &scope_id))
5977+
{
59775978
return NULL;
5979+
}
59785980
if (flowinfo > 0xfffff) {
59795981
PyErr_SetString(PyExc_OverflowError,
5980-
"getsockaddrarg: flowinfo must be 0-1048575.");
5982+
"getnameinfo(): flowinfo must be 0-1048575.");
59815983
return NULL;
59825984
}
59835985
PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);

Modules/timemodule.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ When 'seconds' is not passed in, convert the current time instead.");
415415
* an exception and return 0 on error.
416416
*/
417417
static int
418-
gettmarg(PyObject *args, struct tm *p)
418+
gettmarg(PyObject *args, struct tm *p, const char *format)
419419
{
420420
int y;
421421

@@ -427,7 +427,7 @@ gettmarg(PyObject *args, struct tm *p)
427427
return 0;
428428
}
429429

430-
if (!PyArg_ParseTuple(args, "iiiiiiiii",
430+
if (!PyArg_ParseTuple(args, format,
431431
&y, &p->tm_mon, &p->tm_mday,
432432
&p->tm_hour, &p->tm_min, &p->tm_sec,
433433
&p->tm_wday, &p->tm_yday, &p->tm_isdst))
@@ -586,8 +586,12 @@ time_strftime(PyObject *self, PyObject *args)
586586
if (_PyTime_localtime(tt, &buf) != 0)
587587
return NULL;
588588
}
589-
else if (!gettmarg(tup, &buf) || !checktm(&buf))
589+
else if (!gettmarg(tup, &buf,
590+
"iiiiiiiii;strftime(): illegal time tuple argument") ||
591+
!checktm(&buf))
592+
{
590593
return NULL;
594+
}
591595

592596
#if defined(_MSC_VER) || defined(sun) || defined(_AIX)
593597
if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) {
@@ -776,9 +780,13 @@ time_asctime(PyObject *self, PyObject *args)
776780
time_t tt = time(NULL);
777781
if (_PyTime_localtime(tt, &buf) != 0)
778782
return NULL;
779-
780-
} else if (!gettmarg(tup, &buf) || !checktm(&buf))
783+
}
784+
else if (!gettmarg(tup, &buf,
785+
"iiiiiiiii;asctime(): illegal time tuple argument") ||
786+
!checktm(&buf))
787+
{
781788
return NULL;
789+
}
782790
return _asctime(&buf);
783791
}
784792

@@ -814,8 +822,11 @@ time_mktime(PyObject *self, PyObject *tup)
814822
{
815823
struct tm buf;
816824
time_t tt;
817-
if (!gettmarg(tup, &buf))
825+
if (!gettmarg(tup, &buf,
826+
"iiiiiiiii;mktime(): illegal time tuple argument"))
827+
{
818828
return NULL;
829+
}
819830
#ifdef _AIX
820831
/* year < 1902 or year > 2037 */
821832
if (buf.tm_year < 2 || buf.tm_year > 137) {

0 commit comments

Comments
 (0)