]> granicus.if.org Git - python/commitdiff
Merged revisions 70052 via svnmerge from
authorHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Sat, 28 Feb 2009 10:56:50 +0000 (10:56 +0000)
committerHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Sat, 28 Feb 2009 10:56:50 +0000 (10:56 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r70052 | hirokazu.yamamoto | 2009-02-28 19:31:54 +0900 | 2 lines

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

Lib/test/test_mmap.py
Misc/NEWS
Modules/mmapmodule.c

index 8b7e3e22d03d2e2357218cb0c5b196d7c9685cf7..59b2374ac26310abfe1853d01bcf40bec68c7905 100644 (file)
@@ -466,6 +466,44 @@ class MmapTests(unittest.TestCase):
         self.assert_(issubclass(mmap.error, EnvironmentError))
         self.assert_("mmap.error" in str(mmap.error))
 
+    def test_io_methods(self):
+        data = b"0123456789"
+        open(TESTFN, "wb").write(b"x"*len(data))
+        f = open(TESTFN, "r+b")
+        m = mmap.mmap(f.fileno(), len(data))
+        f.close()
+        # Test write_byte()
+        for i in range(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, b"x")
+        self.assertEquals(m[:], data)
+        # Test read_byte()
+        m.seek(0)
+        for i in range(len(data)):
+            self.assertEquals(m.tell(), i)
+            # XXX: Disable this test for now because it's not clear
+            # which type of object m.read_byte returns. Currently, it
+            # returns 1-length str (unicode).
+            if 0:
+                self.assertEquals(m.read_byte(), data[i:i+1])
+            else:
+                m.read_byte()
+            self.assertEquals(m.tell(), i+1)
+        self.assertRaises(ValueError, m.read_byte)
+        # Test read()
+        m.seek(3)
+        self.assertEquals(m.read(3), b"345")
+        self.assertEquals(m.tell(), 6)
+        # Test write()
+        m.seek(3)
+        m.write(b"bar")
+        self.assertEquals(m.tell(), 6)
+        self.assertEquals(m[:], b"012bar6789")
+        m.seek(8)
+        self.assertRaises(ValueError, m.write, b"bar")
+
 
 def test_main():
     run_unittest(MmapTests)
index d354be87009020b4cc49d8bbefb5109142736736..ff271b98236ba8e867850f39ba34ebe0988d2010 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -173,6 +173,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.
 
index 7fa103779683884f1cdabd978e61aacbd0aef89c..db944066034cea77144701ec3cddc3a37ec291c3 100644 (file)
@@ -376,10 +376,17 @@ mmap_write_byte_method(mmap_object *self,
 
        if (!is_writable(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 *