]> granicus.if.org Git - python/commitdiff
Merged revisions 73677,73681 via svnmerge from
authorHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Mon, 29 Jun 2009 14:37:28 +0000 (14:37 +0000)
committerHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Mon, 29 Jun 2009 14:37:28 +0000 (14:37 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r73677 | hirokazu.yamamoto | 2009-06-29 22:25:16 +0900 | 2 lines

  Issue #6344: Fixed a crash of mmap.read() when passed a negative argument.
  Reviewed by Amaury Forgeot d'Arc.
........
  r73681 | hirokazu.yamamoto | 2009-06-29 23:29:31 +0900 | 1 line

  Fixed NEWS.
........

Misc/NEWS
Modules/mmapmodule.c

index c1ffebe6ca5c72f107c89c488cb9bcd6818363cf..1ba1a73983d8f89252f7742ef17a28c6b117519a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -56,6 +56,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #6344: Fixed a crash of mmap.read() when passed a negative argument.
+
 - Issue #5230: pydoc would report no documentation found if a module generated
   a 'not found' import error when loaded; it now reports the import errors.
   Thanks to Lucas Prado Melo for initial fix and collaboration on the tests.
index 1d0f490d5ab626f1b09c82a7a019dad93c1b1e94..ac572d2fb1db08ccc2e2b88ddbc1541ea9d81e67 100644 (file)
@@ -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;