Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer

overrun.
This commit is contained in:
Hirokazu Yamamoto 2009-02-28 10:31:54 +00:00
parent 7334735ee1
commit f2dc885780
3 changed files with 46 additions and 4 deletions

View File

@ -467,6 +467,38 @@ class MmapTests(unittest.TestCase):
self.assert_(issubclass(mmap.error, EnvironmentError))
self.assert_("mmap.error" in str(mmap.error))
def test_io_methods(self):
data = "0123456789"
open(TESTFN, "wb").write("x"*len(data))
f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), len(data))
f.close()
# Test write_byte()
for i in xrange(len(data)):
self.assertEquals(m.tell(), i)
m.write_byte(data[i:i+1])
self.assertEquals(m.tell(), i+1)
self.assertRaises(ValueError, m.write_byte, "x")
self.assertEquals(m[:], data)
# Test read_byte()
m.seek(0)
for i in xrange(len(data)):
self.assertEquals(m.tell(), i)
self.assertEquals(m.read_byte(), data[i:i+1])
self.assertEquals(m.tell(), i+1)
self.assertRaises(ValueError, m.read_byte)
# Test read()
m.seek(3)
self.assertEquals(m.read(3), "345")
self.assertEquals(m.tell(), 6)
# Test write()
m.seek(3)
m.write("bar")
self.assertEquals(m.tell(), 6)
self.assertEquals(m[:], "012bar6789")
m.seek(8)
self.assertRaises(ValueError, m.write, "bar")
def test_main():
run_unittest(MmapTests)

View File

@ -166,6 +166,9 @@ Core and Builtins
Library
-------
- Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
overrun.
- Issue #1533164: Installed but not listed *.pyo was breaking Distutils
bdist_rpm command.

View File

@ -365,10 +365,17 @@ mmap_write_byte_method(mmap_object *self,
if (!is_writeable(self))
return NULL;
*(self->data+self->pos) = value;
self->pos += 1;
Py_INCREF(Py_None);
return Py_None;
if (self->pos < self->size) {
*(self->data+self->pos) = value;
self->pos += 1;
Py_INCREF(Py_None);
return Py_None;
}
else {
PyErr_SetString(PyExc_ValueError, "write byte out of range");
return NULL;
}
}
static PyObject *