Issue #17671: Fixed a crash when use non-initialized io.BufferedRWPair.

Based on patch by Stephen Tu.
This commit is contained in:
Serhiy Storchaka 2014-02-12 10:55:07 +02:00
commit 5bdfc51950
3 changed files with 48 additions and 2 deletions

View File

@ -855,6 +855,16 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
bufio.__init__(rawio)
self.assertEqual(b"abc", bufio.read())
def test_uninitialized(self):
bufio = self.tp.__new__(self.tp)
del bufio
bufio = self.tp.__new__(self.tp)
self.assertRaisesRegex((ValueError, AttributeError),
'uninitialized|has no attribute',
bufio.read, 0)
bufio.__init__(self.MockRawIO())
self.assertEqual(bufio.read(0), b'')
def test_read(self):
for arg in (None, 7):
rawio = self.MockRawIO((b"abc", b"d", b"efg"))
@ -1106,6 +1116,16 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
bufio.flush()
self.assertEqual(b"".join(rawio._write_stack), b"abcghi")
def test_uninitialized(self):
bufio = self.tp.__new__(self.tp)
del bufio
bufio = self.tp.__new__(self.tp)
self.assertRaisesRegex((ValueError, AttributeError),
'uninitialized|has no attribute',
bufio.write, b'')
bufio.__init__(self.MockRawIO())
self.assertEqual(bufio.write(b''), 0)
def test_detach_flush(self):
raw = self.MockRawIO()
buf = self.tp(raw)
@ -1390,6 +1410,20 @@ class BufferedRWPairTest(unittest.TestCase):
pair = self.tp(self.MockRawIO(), self.MockRawIO())
self.assertFalse(pair.closed)
def test_uninitialized(self):
pair = self.tp.__new__(self.tp)
del pair
pair = self.tp.__new__(self.tp)
self.assertRaisesRegex((ValueError, AttributeError),
'uninitialized|has no attribute',
pair.read, 0)
self.assertRaisesRegex((ValueError, AttributeError),
'uninitialized|has no attribute',
pair.write, b'')
pair.__init__(self.MockRawIO(), self.MockRawIO())
self.assertEqual(pair.read(0), b'')
self.assertEqual(pair.write(b''), 0)
def test_detach(self):
pair = self.tp(self.MockRawIO(), self.MockRawIO())
self.assertRaises(self.UnsupportedOperation, pair.detach)
@ -1516,6 +1550,10 @@ class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest):
BufferedReaderTest.test_constructor(self)
BufferedWriterTest.test_constructor(self)
def test_uninitialized(self):
BufferedReaderTest.test_uninitialized(self)
BufferedWriterTest.test_uninitialized(self)
def test_read_and_write(self):
raw = self.MockRawIO((b"asdf", b"ghjk"))
rw = self.tp(raw, 8)

View File

@ -15,6 +15,9 @@ Core and Builtins
Library
-------
- Issue #17671: Fixed a crash when use non-initialized io.BufferedRWPair.
Based on patch by Stephen Tu.
- Issue #20594: Avoid name clash with the libc function posix_close.
- Issue #19856: shutil.move() failed to move a directory to other directory

View File

@ -2305,9 +2305,14 @@ bufferedrwpair_dealloc(rwpair *self)
static PyObject *
_forward_call(buffered *self, _Py_Identifier *name, PyObject *args)
{
PyObject *func = _PyObject_GetAttrId((PyObject *)self, name);
PyObject *ret;
PyObject *func, *ret;
if (self == NULL) {
PyErr_SetString(PyExc_ValueError,
"I/O operation on uninitialized object");
return NULL;
}
func = _PyObject_GetAttrId((PyObject *)self, name);
if (func == NULL) {
PyErr_SetString(PyExc_AttributeError, name->string);
return NULL;