From f6bbd0e71d986e0acefb4b3708d9129a9bce4a17 Mon Sep 17 00:00:00 2001 From: Hirokazu Yamamoto Date: Tue, 17 Feb 2009 10:12:10 +0000 Subject: [PATCH] Issue #5292: Fixed mmap crash on its boundary access m[len(m)]. --- Lib/test/test_mmap.py | 4 ++++ Misc/NEWS | 2 ++ Modules/mmapmodule.c | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 6fecaf565b2..e1e59670c6c 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -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' diff --git a/Misc/NEWS b/Misc/NEWS index 004e9f7f894..3b3f9d83e5c 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -159,6 +159,8 @@ Core and Builtins Library ------- +- Issue #5292: Fixed mmap crash on its boundary access m[len(m)]. + - Issue #2279: distutils.sdist.add_defaults now add files from the package_data and the data_files metadata. diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index a03957cf6fa..df2c058ec3f 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -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;