summaryrefslogtreecommitdiffstats
path: root/Modules/mmapmodule.c
diff options
context:
space:
mode:
authorHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>2009-06-29 13:25:16 (GMT)
committerHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>2009-06-29 13:25:16 (GMT)
commit8839fd7372a373c0d63d56897c2593fff5738df0 (patch)
treef394fc3e3406bf0eaa0d93cddc04b0492514cb26 /Modules/mmapmodule.c
parentbcff47a6ec43e945a74e202f987bbce9e40fade7 (diff)
downloadcpython-8839fd7372a373c0d63d56897c2593fff5738df0.zip
cpython-8839fd7372a373c0d63d56897c2593fff5738df0.tar.gz
cpython-8839fd7372a373c0d63d56897c2593fff5738df0.tar.bz2
Issue #6344: Fixed a crash of mmap.read() when passed a negative argument.
Reviewed by Amaury Forgeot d'Arc.
Diffstat (limited to 'Modules/mmapmodule.c')
-rw-r--r--Modules/mmapmodule.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index e1970cb..5e0b3ad 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -232,7 +232,7 @@ static PyObject *
mmap_read_method(mmap_object *self,
PyObject *args)
{
- Py_ssize_t num_bytes;
+ Py_ssize_t num_bytes, n;
PyObject *result;
CHECK_VALID(NULL);
@@ -240,8 +240,18 @@ mmap_read_method(mmap_object *self,
return(NULL);
/* silently 'adjust' out-of-range requests */
- if (num_bytes > self->size - self->pos) {
- num_bytes -= (self->pos+num_bytes) - self->size;
+ assert(self->size >= self->pos);
+ n = self->size - self->pos;
+ /* The difference can overflow, only if self->size is greater than
+ * PY_SSIZE_T_MAX. But then the operation cannot possibly succeed,
+ * because the mapped area and the returned string each need more
+ * than half of the addressable memory. So we clip the size, and let
+ * the code below raise MemoryError.
+ */
+ if (n < 0)
+ n = PY_SSIZE_T_MAX;
+ if (num_bytes < 0 || num_bytes > n) {
+ num_bytes = n;
}
result = Py_BuildValue("s#", self->data+self->pos, num_bytes);
self->pos += num_bytes;