summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-02-12 08:52:35 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-02-12 08:52:35 (GMT)
commit61e2493b8341be74928872ce6d7fb3a350bd1697 (patch)
treea9e16cf785c8cd4c54d79c633e11fa64c9857672
parent1f9d11b986c95005fbebb724ee3425ad2eb30967 (diff)
downloadcpython-61e2493b8341be74928872ce6d7fb3a350bd1697.zip
cpython-61e2493b8341be74928872ce6d7fb3a350bd1697.tar.gz
cpython-61e2493b8341be74928872ce6d7fb3a350bd1697.tar.bz2
Issue #17671: Fixed a crash when use non-initialized io.BufferedRWPair.
Based on patch by Stephen Tu.
-rw-r--r--Lib/test/test_io.py38
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_io/bufferedio.c9
3 files changed, 48 insertions, 2 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 5c2c79b..ac6d478 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -846,6 +846,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"))
@@ -1096,6 +1106,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)
@@ -1379,6 +1399,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)
@@ -1505,6 +1539,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)
diff --git a/Misc/NEWS b/Misc/NEWS
index bf0bc0c..654f7e6 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -20,6 +20,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
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 3afe7b1..c28e289 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -2263,9 +2263,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;