]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 69714,69718 via svnmerge from
authorHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Wed, 18 Feb 2009 15:11:55 +0000 (15:11 +0000)
committerHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Wed, 18 Feb 2009 15:11:55 +0000 (15:11 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r69714 | hirokazu.yamamoto | 2009-02-17 19:12:10 +0900 | 1 line

  Issue #5292: Fixed mmap crash on its boundary access m[len(m)].
........
  r69718 | hirokazu.yamamoto | 2009-02-17 22:17:26 +0900 | 3 lines

  Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
  The file was resized to wrong size.
........

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

index 6fecaf565b295365b0cc5b1d778a5d52ccfc4f33..c06b7579ccd764b2ea73343a364765e21abc7e24 100644 (file)
@@ -41,6 +41,10 @@ class MmapTests(unittest.TestCase):
             self.assertEqual(m[0], '\0')
             self.assertEqual(m[0:3], '\0\0\0')
 
+            # Shouldn't crash on boundary (Issue #5292)
+            self.assertRaises(IndexError, m.__getitem__, len(m))
+            self.assertRaises(IndexError, m.__setitem__, len(m), '\0')
+
             # Modify the file's content
             m[0] = '3'
             m[PAGESIZE +3: PAGESIZE +3+3] = 'bar'
@@ -413,6 +417,27 @@ class MmapTests(unittest.TestCase):
             m = mmap.mmap(f.fileno(), mapsize - halfsize, offset=halfsize)
             self.assertEqual(m[0:3], 'foo')
             f.close()
+
+            # Try resizing map
+            try:
+                m.resize(512)
+            except SystemError:
+                pass
+            else:
+                # resize() is supported
+                self.assertEqual(len(m), 512)
+                # Check that we can no longer seek beyond the new size.
+                self.assertRaises(ValueError, m.seek, 513, 0)
+                # Check that the content is not changed
+                self.assertEqual(m[0:3], 'foo')
+
+                # Check that the underlying file is truncated too
+                f = open(TESTFN)
+                f.seek(0, 2)
+                self.assertEqual(f.tell(), halfsize + 512)
+                f.close()
+                self.assertEqual(m.size(), halfsize + 512)
+
             m.close()
 
         finally:
index 45d145e9209c4c4427963fccf581d5045fab5c01..2ebd6cc32550761e67f4415ffb17a25b3f05b4a1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -80,6 +80,11 @@ Core and Builtins
 Library
 -------
 
+- Issue #5292: Fixed mmap crash on its boundary access m[len(m)].
+
+- Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
+  The file was resized to wrong size.
+
 - Issue #5287: Add exception handling around findCaller() call in logging to
   help out IronPython.
 
index a03957cf6fa402bbceb4781a54b2e5708ca1dde3..fad60d109f8c8c304ab614e2dd1e33f1c02b95d3 100644 (file)
@@ -444,7 +444,7 @@ mmap_resize_method(mmap_object *self,
                off_lo = (DWORD)(self->offset & 0xFFFFFFFF);
 #else
                newSizeHigh = 0;
-               newSizeLow = (DWORD)new_size;
+               newSizeLow = (DWORD)(self->offset + new_size);
                off_hi = 0;
                off_lo = (DWORD)self->offset;
 #endif
@@ -490,7 +490,7 @@ mmap_resize_method(mmap_object *self,
        } else {
                void *newmap;
 
-               if (ftruncate(self->fd, new_size) == -1) {
+               if (ftruncate(self->fd, self->offset + new_size) == -1) {
                        PyErr_SetFromErrno(mmap_module_error);
                        return NULL;
                }
@@ -731,7 +731,7 @@ mmap_subscript(mmap_object *self, PyObject *item)
                        return NULL;
                if (i < 0)
                        i += self->size;
-               if (i < 0 || (size_t)i > self->size) {
+               if (i < 0 || (size_t)i >= self->size) {
                        PyErr_SetString(PyExc_IndexError,
                                "mmap index out of range");
                        return NULL;
@@ -872,7 +872,7 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
                        return -1;
                if (i < 0)
                        i += self->size;
-               if (i < 0 || (size_t)i > self->size) {
+               if (i < 0 || (size_t)i >= self->size) {
                        PyErr_SetString(PyExc_IndexError,
                                "mmap index out of range");
                        return -1;