mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
#15676: mmap: add empty file check prior to offset check <- Previous patch was incomplete
This commit is contained in:
parent
3fb774ec5f
commit
20f0ea1f61
2 changed files with 14 additions and 12 deletions
|
@ -469,18 +469,15 @@ class MmapTests(unittest.TestCase):
|
||||||
def test_empty_file (self):
|
def test_empty_file (self):
|
||||||
f = open (TESTFN, 'w+b')
|
f = open (TESTFN, 'w+b')
|
||||||
f.close()
|
f.close()
|
||||||
f = open(TESTFN, "rb")
|
with open(TESTFN, "rb") as f :
|
||||||
try:
|
try:
|
||||||
m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
|
m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
|
||||||
m.close()
|
m.close()
|
||||||
f.close()
|
self.fail("should not have been able to mmap empty file")
|
||||||
self.fail("should not have been able to mmap empty file")
|
except ValueError as e:
|
||||||
except ValueError as e:
|
self.assertEqual(e.message, "cannot mmap an empty file")
|
||||||
f.close()
|
except:
|
||||||
self.assertEqual(e.message, "cannot mmap an empty file")
|
self.fail("unexpected exception: " + str(e))
|
||||||
except:
|
|
||||||
f.close()
|
|
||||||
self.fail("unexpected exception: " + str(e))
|
|
||||||
|
|
||||||
def test_offset (self):
|
def test_offset (self):
|
||||||
f = open (TESTFN, 'w+b')
|
f = open (TESTFN, 'w+b')
|
||||||
|
|
|
@ -1388,6 +1388,11 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
|
||||||
}
|
}
|
||||||
|
|
||||||
size = (((PY_LONG_LONG) high) << 32) + low;
|
size = (((PY_LONG_LONG) high) << 32) + low;
|
||||||
|
if (size == 0) {
|
||||||
|
PyErr_SetString(PyExc_ValueError,
|
||||||
|
"cannot mmap an empty file");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
if (offset >= size) {
|
if (offset >= size) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"mmap offset is greater than file size");
|
"mmap offset is greater than file size");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue