Skip to content

Commit ee06fff

Browse files
gh-111942: Fix crashes in TextIOWrapper.reconfigure() (GH-111976)
* Fix crash when encoding is not string or None. * Fix crash when both line_buffering and write_through raise exception when converted ti int. * Add a number of tests for constructor and reconfigure() method with invalid arguments.
1 parent a519b87 commit ee06fff

File tree

3 files changed

+122
-5
lines changed

3 files changed

+122
-5
lines changed

Lib/test/test_io.py

+84-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def _default_chunk_size():
7777
)
7878

7979

80+
class BadIndex:
81+
def __index__(self):
82+
1/0
83+
8084
class MockRawIOWithoutRead:
8185
"""A RawIO implementation without read(), so as to exercise the default
8286
RawIO.read() which calls readinto()."""
@@ -2709,8 +2713,31 @@ def test_constructor(self):
27092713
self.assertEqual(t.encoding, "utf-8")
27102714
self.assertEqual(t.line_buffering, True)
27112715
self.assertEqual("\xe9\n", t.readline())
2712-
self.assertRaises(TypeError, t.__init__, b, encoding="utf-8", newline=42)
2713-
self.assertRaises(ValueError, t.__init__, b, encoding="utf-8", newline='xyzzy')
2716+
invalid_type = TypeError if self.is_C else ValueError
2717+
with self.assertRaises(invalid_type):
2718+
t.__init__(b, encoding=42)
2719+
with self.assertRaises(UnicodeEncodeError):
2720+
t.__init__(b, encoding='\udcfe')
2721+
with self.assertRaises(ValueError):
2722+
t.__init__(b, encoding='utf-8\0')
2723+
with self.assertRaises(invalid_type):
2724+
t.__init__(b, encoding="utf-8", errors=42)
2725+
if support.Py_DEBUG or sys.flags.dev_mode or self.is_C:
2726+
with self.assertRaises(UnicodeEncodeError):
2727+
t.__init__(b, encoding="utf-8", errors='\udcfe')
2728+
if support.Py_DEBUG or sys.flags.dev_mode:
2729+
# TODO: If encoded to UTF-8, should also be checked for
2730+
# embedded null characters.
2731+
with self.assertRaises(ValueError):
2732+
t.__init__(b, encoding="utf-8", errors='replace\0')
2733+
with self.assertRaises(TypeError):
2734+
t.__init__(b, encoding="utf-8", newline=42)
2735+
with self.assertRaises(ValueError):
2736+
t.__init__(b, encoding="utf-8", newline='\udcfe')
2737+
with self.assertRaises(ValueError):
2738+
t.__init__(b, encoding="utf-8", newline='\n\0')
2739+
with self.assertRaises(ValueError):
2740+
t.__init__(b, encoding="utf-8", newline='xyzzy')
27142741

27152742
def test_uninitialized(self):
27162743
t = self.TextIOWrapper.__new__(self.TextIOWrapper)
@@ -3756,6 +3783,59 @@ def test_reconfigure_defaults(self):
37563783

37573784
self.assertEqual(txt.detach().getvalue(), b'LF\nCRLF\r\n')
37583785

3786+
def test_reconfigure_errors(self):
3787+
txt = self.TextIOWrapper(self.BytesIO(), 'ascii', 'replace', '\r')
3788+
with self.assertRaises(TypeError): # there was a crash
3789+
txt.reconfigure(encoding=42)
3790+
if self.is_C:
3791+
with self.assertRaises(UnicodeEncodeError):
3792+
txt.reconfigure(encoding='\udcfe')
3793+
with self.assertRaises(LookupError):
3794+
txt.reconfigure(encoding='locale\0')
3795+
# TODO: txt.reconfigure(encoding='utf-8\0')
3796+
# TODO: txt.reconfigure(encoding='nonexisting')
3797+
with self.assertRaises(TypeError):
3798+
txt.reconfigure(errors=42)
3799+
if self.is_C:
3800+
with self.assertRaises(UnicodeEncodeError):
3801+
txt.reconfigure(errors='\udcfe')
3802+
# TODO: txt.reconfigure(errors='ignore\0')
3803+
# TODO: txt.reconfigure(errors='nonexisting')
3804+
with self.assertRaises(TypeError):
3805+
txt.reconfigure(newline=42)
3806+
with self.assertRaises(ValueError):
3807+
txt.reconfigure(newline='\udcfe')
3808+
with self.assertRaises(ValueError):
3809+
txt.reconfigure(newline='xyz')
3810+
if not self.is_C:
3811+
# TODO: Should fail in C too.
3812+
with self.assertRaises(ValueError):
3813+
txt.reconfigure(newline='\n\0')
3814+
if self.is_C:
3815+
# TODO: Use __bool__(), not __index__().
3816+
with self.assertRaises(ZeroDivisionError):
3817+
txt.reconfigure(line_buffering=BadIndex())
3818+
with self.assertRaises(OverflowError):
3819+
txt.reconfigure(line_buffering=2**1000)
3820+
with self.assertRaises(ZeroDivisionError):
3821+
txt.reconfigure(write_through=BadIndex())
3822+
with self.assertRaises(OverflowError):
3823+
txt.reconfigure(write_through=2**1000)
3824+
with self.assertRaises(ZeroDivisionError): # there was a crash
3825+
txt.reconfigure(line_buffering=BadIndex(),
3826+
write_through=BadIndex())
3827+
self.assertEqual(txt.encoding, 'ascii')
3828+
self.assertEqual(txt.errors, 'replace')
3829+
self.assertIs(txt.line_buffering, False)
3830+
self.assertIs(txt.write_through, False)
3831+
3832+
txt.reconfigure(encoding='latin1', errors='ignore', newline='\r\n',
3833+
line_buffering=True, write_through=True)
3834+
self.assertEqual(txt.encoding, 'latin1')
3835+
self.assertEqual(txt.errors, 'ignore')
3836+
self.assertIs(txt.line_buffering, True)
3837+
self.assertIs(txt.write_through, True)
3838+
37593839
def test_reconfigure_newline(self):
37603840
raw = self.BytesIO(b'CR\rEOF')
37613841
txt = self.TextIOWrapper(raw, 'ascii', newline='\n')
@@ -4781,9 +4861,11 @@ def load_tests(loader, tests, pattern):
47814861
if test.__name__.startswith("C"):
47824862
for name, obj in c_io_ns.items():
47834863
setattr(test, name, obj)
4864+
test.is_C = True
47844865
elif test.__name__.startswith("Py"):
47854866
for name, obj in py_io_ns.items():
47864867
setattr(test, name, obj)
4868+
test.is_C = False
47874869

47884870
suite = loader.suiteClass()
47894871
for test in tests:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crashes in :meth:`io.TextIOWrapper.reconfigure` when pass invalid
2+
arguments, e.g. non-string encoding.

Modules/_io/textio.c

+36-3
Original file line numberDiff line numberDiff line change
@@ -1284,30 +1284,40 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,
12841284
errors = &_Py_ID(strict);
12851285
}
12861286
}
1287+
Py_INCREF(errors);
12871288

1289+
const char *c_encoding = PyUnicode_AsUTF8(encoding);
1290+
if (c_encoding == NULL) {
1291+
Py_DECREF(encoding);
1292+
Py_DECREF(errors);
1293+
return -1;
1294+
}
12881295
const char *c_errors = PyUnicode_AsUTF8(errors);
12891296
if (c_errors == NULL) {
12901297
Py_DECREF(encoding);
1298+
Py_DECREF(errors);
12911299
return -1;
12921300
}
12931301

12941302
// Create new encoder & decoder
12951303
PyObject *codec_info = _PyCodec_LookupTextEncoding(
1296-
PyUnicode_AsUTF8(encoding), "codecs.open()");
1304+
c_encoding, "codecs.open()");
12971305
if (codec_info == NULL) {
12981306
Py_DECREF(encoding);
1307+
Py_DECREF(errors);
12991308
return -1;
13001309
}
13011310
if (_textiowrapper_set_decoder(self, codec_info, c_errors) != 0 ||
13021311
_textiowrapper_set_encoder(self, codec_info, c_errors) != 0) {
13031312
Py_DECREF(codec_info);
13041313
Py_DECREF(encoding);
1314+
Py_DECREF(errors);
13051315
return -1;
13061316
}
13071317
Py_DECREF(codec_info);
13081318

13091319
Py_SETREF(self->encoding, encoding);
1310-
Py_SETREF(self->errors, Py_NewRef(errors));
1320+
Py_SETREF(self->errors, errors);
13111321

13121322
return _textiowrapper_fix_encoder_state(self);
13131323
}
@@ -1338,6 +1348,26 @@ _io_TextIOWrapper_reconfigure_impl(textio *self, PyObject *encoding,
13381348
int write_through;
13391349
const char *newline = NULL;
13401350

1351+
if (encoding != Py_None && !PyUnicode_Check(encoding)) {
1352+
PyErr_Format(PyExc_TypeError,
1353+
"reconfigure() argument 'encoding' must be str or None, not %s",
1354+
Py_TYPE(encoding)->tp_name);
1355+
return NULL;
1356+
}
1357+
if (errors != Py_None && !PyUnicode_Check(errors)) {
1358+
PyErr_Format(PyExc_TypeError,
1359+
"reconfigure() argument 'errors' must be str or None, not %s",
1360+
Py_TYPE(errors)->tp_name);
1361+
return NULL;
1362+
}
1363+
if (newline_obj != NULL && newline_obj != Py_None &&
1364+
!PyUnicode_Check(newline_obj))
1365+
{
1366+
PyErr_Format(PyExc_TypeError,
1367+
"reconfigure() argument 'newline' must be str or None, not %s",
1368+
Py_TYPE(newline_obj)->tp_name);
1369+
return NULL;
1370+
}
13411371
/* Check if something is in the read buffer */
13421372
if (self->decoded_chars != NULL) {
13431373
if (encoding != Py_None || errors != Py_None || newline_obj != NULL) {
@@ -1357,9 +1387,12 @@ _io_TextIOWrapper_reconfigure_impl(textio *self, PyObject *encoding,
13571387

13581388
line_buffering = convert_optional_bool(line_buffering_obj,
13591389
self->line_buffering);
1390+
if (line_buffering < 0) {
1391+
return NULL;
1392+
}
13601393
write_through = convert_optional_bool(write_through_obj,
13611394
self->write_through);
1362-
if (line_buffering < 0 || write_through < 0) {
1395+
if (write_through < 0) {
13631396
return NULL;
13641397
}
13651398

0 commit comments

Comments
 (0)