From 2ccf6203cb9483d333a1050509cddcd2bd698950 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sat, 14 Jul 2018 08:59:24 -0600 Subject: [PATCH 1/2] bpo-34068: _io__IOBase_close_impl could call _PyObject_SetAttrId with an exception set --- Lib/test/test_io.py | 10 ++++++++++ .../2018-07-14-08-58-46.bpo-34068.9xfM55.rst | 2 ++ Modules/_io/iobase.c | 12 +++++++----- 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-07-14-08-58-46.bpo-34068.9xfM55.rst diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index a03a7f78109c23..c68b2fea858b71 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -968,6 +968,16 @@ def read1(self, size): self.assertSequenceEqual(buffer[result:], unused) self.assertEqual(len(reader.avail), avail - result) + def test_close_assert(self): + class R(self.IOBase): + def __setattr__(self, name, value): + pass + def flush(self): + raise OSError() + f = R() + # This would cause an assertion failure. + self.assertRaises(OSError, f.close) + class CIOTest(IOTest): diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-07-14-08-58-46.bpo-34068.9xfM55.rst b/Misc/NEWS.d/next/Core and Builtins/2018-07-14-08-58-46.bpo-34068.9xfM55.rst new file mode 100644 index 00000000000000..44abe0ca755606 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-07-14-08-58-46.bpo-34068.9xfM55.rst @@ -0,0 +1,2 @@ +In :meth:`io.IOBase.close`, ensure that ``_PyObject_SetAttrId()`` is not +called with a live exception. Patch by Zackery Spytz and Serhiy Storchaka. diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c index 0c329bff840bd3..5b71732ef19c65 100644 --- a/Modules/_io/iobase.c +++ b/Modules/_io/iobase.c @@ -224,8 +224,8 @@ static PyObject * _io__IOBase_close_impl(PyObject *self) /*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/ { - PyObject *res; - int closed = iobase_is_closed(self); + PyObject *res, *exc, *val, *tb; + int rc, closed = iobase_is_closed(self); if (closed < 0) { return NULL; @@ -236,9 +236,11 @@ _io__IOBase_close_impl(PyObject *self) res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL); - if (_PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True) < 0) { - Py_XDECREF(res); - return NULL; + PyErr_Fetch(&exc, &val, &tb); + rc = _PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True); + _PyErr_ChainExceptions(exc, val, tb); + if (rc < 0) { + Py_CLEAR(res); } if (res == NULL) From 55034f1f06f276330a038bab0e5aec3290c2005e Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 16 Jul 2018 23:08:17 -0600 Subject: [PATCH 2/2] Tweak the news entry. --- .../2018-07-14-08-58-46.bpo-34068.9xfM55.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-07-14-08-58-46.bpo-34068.9xfM55.rst b/Misc/NEWS.d/next/Core and Builtins/2018-07-14-08-58-46.bpo-34068.9xfM55.rst index 44abe0ca755606..0ed8ff91925ab5 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2018-07-14-08-58-46.bpo-34068.9xfM55.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2018-07-14-08-58-46.bpo-34068.9xfM55.rst @@ -1,2 +1,3 @@ -In :meth:`io.IOBase.close`, ensure that ``_PyObject_SetAttrId()`` is not -called with a live exception. Patch by Zackery Spytz and Serhiy Storchaka. +In :meth:`io.IOBase.close`, ensure that the :attr:`~io.IOBase.closed` +attribute is not set with a live exception. Patch by Zackery Spytz and Serhiy +Storchaka.