Skip to content

Commit

Permalink
bpo-25455: Fixed crashes in repr of recursive buffered file-like obje…
Browse files Browse the repository at this point in the history
…cts. (python#514)

(cherry picked from commit a5af6e1)
  • Loading branch information
serhiy-storchaka committed Mar 19, 2017
1 parent bb67f10 commit afb8245
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
9 changes: 8 additions & 1 deletion Lib/test/test_fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from weakref import proxy
from functools import wraps

from test.support import TESTFN, check_warnings, run_unittest, make_bad_fd, cpython_only
from test.support import (TESTFN, check_warnings, run_unittest,
make_bad_fd, cpython_only, swap_attr)
from collections import UserList

import _io # C implementation of io
Expand Down Expand Up @@ -175,6 +176,12 @@ def testReprNoCloseFD(self):
finally:
os.close(fd)

def testRecursiveRepr(self):
# Issue #25455
with swap_attr(self.f, 'name', self.f):
with self.assertRaises(RuntimeError):
repr(self.f) # Should not crash

def testErrors(self):
f = self.f
self.assertFalse(f.isatty())
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,16 @@ def test_repr(self):
raw.name = b"dummy"
self.assertEqual(repr(b), "<%s name=b'dummy'>" % clsname)

def test_recursive_repr(self):
# Issue #25455
raw = self.MockRawIO()
b = self.tp(raw)
with support.swap_attr(raw, 'name', b):
try:
repr(b) # Should not crash
except RuntimeError:
pass

def test_flush_error_on_close(self):
# Test that buffered file is closed despite failed flush
# and that flush() is called before file closed.
Expand Down Expand Up @@ -2391,6 +2401,16 @@ def test_repr(self):
t.buffer.detach()
repr(t) # Should not raise an exception

def test_recursive_repr(self):
# Issue #25455
raw = self.BytesIO()
t = self.TextIOWrapper(raw)
with support.swap_attr(raw, 'name', t):
try:
repr(t) # Should not crash
except RuntimeError:
pass

def test_line_buffering(self):
r = self.BytesIO()
b = self.BufferedWriter(r, 1000)
Expand Down
2 changes: 2 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Extension Modules
Library
-------

- bpo-25455: Fixed crashes in repr of recursive buffered file-like objects.

- bpo-29800: Fix crashes in partial.__repr__ if the keys of partial.keywords
are not strings. Patch by Michael Seifert.

Expand Down
14 changes: 12 additions & 2 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1416,8 +1416,18 @@ buffered_repr(buffered *self)
res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name);
}
else {
res = PyUnicode_FromFormat("<%s name=%R>",
Py_TYPE(self)->tp_name, nameobj);
int status = Py_ReprEnter((PyObject *)self);
res = NULL;
if (status == 0) {
res = PyUnicode_FromFormat("<%s name=%R>",
Py_TYPE(self)->tp_name, nameobj);
Py_ReprLeave((PyObject *)self);
}
else if (status > 0) {
PyErr_Format(PyExc_RuntimeError,
"reentrant call inside %s.__repr__",
Py_TYPE(self)->tp_name);
}
Py_DECREF(nameobj);
}
return res;
Expand Down
16 changes: 13 additions & 3 deletions Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1093,9 +1093,19 @@ fileio_repr(fileio *self)
self->fd, mode_string(self), self->closefd ? "True" : "False");
}
else {
res = PyUnicode_FromFormat(
"<_io.FileIO name=%R mode='%s' closefd=%s>",
nameobj, mode_string(self), self->closefd ? "True" : "False");
int status = Py_ReprEnter((PyObject *)self);
res = NULL;
if (status == 0) {
res = PyUnicode_FromFormat(
"<_io.FileIO name=%R mode='%s' closefd=%s>",
nameobj, mode_string(self), self->closefd ? "True" : "False");
Py_ReprLeave((PyObject *)self);
}
else if (status > 0) {
PyErr_Format(PyExc_RuntimeError,
"reentrant call inside %s.__repr__",
Py_TYPE(self)->tp_name);
}
Py_DECREF(nameobj);
}
return res;
Expand Down
23 changes: 20 additions & 3 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2483,13 +2483,23 @@ static PyObject *
textiowrapper_repr(textio *self)
{
PyObject *nameobj, *modeobj, *res, *s;
int status;

CHECK_INITIALIZED(self);

res = PyUnicode_FromString("<_io.TextIOWrapper");
if (res == NULL)
return NULL;

status = Py_ReprEnter((PyObject *)self);
if (status != 0) {
if (status > 0) {
PyErr_Format(PyExc_RuntimeError,
"reentrant call inside %s.__repr__",
Py_TYPE(self)->tp_name);
}
goto error;
}
nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
if (nameobj == NULL) {
if (PyErr_ExceptionMatches(PyExc_Exception))
Expand All @@ -2504,7 +2514,7 @@ textiowrapper_repr(textio *self)
goto error;
PyUnicode_AppendAndDel(&res, s);
if (res == NULL)
return NULL;
goto error;
}
modeobj = _PyObject_GetAttrId((PyObject *) self, &PyId_mode);
if (modeobj == NULL) {
Expand All @@ -2520,14 +2530,21 @@ textiowrapper_repr(textio *self)
goto error;
PyUnicode_AppendAndDel(&res, s);
if (res == NULL)
return NULL;
goto error;
}
s = PyUnicode_FromFormat("%U encoding=%R>",
res, self->encoding);
Py_DECREF(res);
if (status == 0) {
Py_ReprLeave((PyObject *)self);
}
return s;
error:

error:
Py_XDECREF(res);
if (status == 0) {
Py_ReprLeave((PyObject *)self);
}
return NULL;
}

Expand Down

0 comments on commit afb8245

Please sign in to comment.