8000 [3.6] bpo-28261: Prevent raising SystemError where PyArg_ParseTuple i… · python/cpython@8e67981 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e67981

Browse files
orenmnserhiy-storchaka
authored andcommitted
[3.6] bpo-28261: Prevent raising SystemError where PyArg_ParseTuple is used to parse non-args. (#3210)
1 parent cb7fdf6 commit 8e67981

File tree

4 files changed

+ 8000 33
-2
lines changed

4 files changed

+33
-2
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
@@ -3410,6 +3410,7 @@ def test_newline_decoder(self):
34103410
decoder = codecs.getincrementaldecoder("utf-8")()
34113411
decoder = self.IncrementalNewlineDecoder(decoder, translate=True)
34123412
self.check_newline_decoding_utf8(decoder)
3413+
self.assertRaises(TypeError, decoder.setstate, 42)
34133414

34143415
def test_newline_bytes 10000 (self):
34153416
# Issue 5433: Excessive optimization in IncrementalNewlineDecoder

Modules/_io/textio.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,12 @@ _io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self)
538538
_PyIO_str_getstate, NULL);
539539
if (state == NULL)
540540
return NULL;
541+
if (!PyTuple_Check(state)) {
542+
PyErr_SetString(PyExc_TypeError,
543+
"illegal decoder state");
544+
Py_DECREF(state);
545+
return NULL;
546+
}
541547
if (!PyArg_ParseTuple(state, "OK", &buffer, &flag)) {
542548
Py_DECREF(state);
543549
return NULL;
@@ -569,6 +575,10 @@ _io_IncrementalNewlineDecoder_setstate(nldecoder_object *self,
569575
PyObject *buffer;
570576
unsigned long long flag;
571577

578+
if (!PyTuple_Check(state)) {
579+
PyErr_SetString(PyExc_TypeError, "state argument must be a tuple");
580+
return NULL;
581+
}
572582
if (!PyArg_ParseTuple(state, "OK", &buffer, &flag))
573583
return NULL;
574584

@@ -2320,6 +2330,12 @@ _io_TextIOWrapper_tell_impl(textio *self)
23202330
_PyIO_str_getstate, NULL); \
23212331
if (_state == NULL) \
23222332
goto fail; \
2333+
if (!PyTuple_Check(_state)) { \
2334+
PyErr_SetString(PyExc_TypeError, \
2335+
"illegal decoder state"); \
2336+
Py_DECREF(_state); \
2337+
goto fail; \
2338+
} \
23232339
if (!PyArg_ParseTuple(_state, "Oi", &dec_buffer, &dec_flags)) { \
23242340
Py_DECREF(_state); \
23252341
goto fail; \

Modules/audioop.c

Lines changed: 12 additions & 2 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,6 +1354,10 @@ 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,
13581362
"iO!;audioop.ratecv: illegal state argument",
13591363
&d, &PyTuple_Type, &samps))
@@ -1364,7 +1368,13 @@ 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),
1371+
channel = PyTuple_GetItem(samps, chan);
1372+
if (!PyTuple_Check(channel)) {
1373+
PyErr_SetString(PyExc_TypeError,
1374+
"ratecv(): illegal state argument");
1375+
goto exit;
1376+
}
1377+
if (!PyArg_ParseTuple(channel,
13681378
"ii:ratecv", &prev_i[chan],
13691379
&cur_i[chan]))
13701380
goto exit;

0 commit comments

Comments
 (0)
0