[Python-checkins] r73677 - in python/trunk: Misc/NEWS Modules/mmapmodule.c

hirokazu.yamamoto python-checkins at python.org
Mon Jun 29 15:25:17 CEST 2009


Author: hirokazu.yamamoto
Date: Mon Jun 29 15:25:16 2009
New Revision: 73677

Log:
Issue #6344: Fixed a crash of mmap.read() when passed a negative argument.
Reviewed by Amaury Forgeot d'Arc.

Modified:
   python/trunk/Misc/NEWS
   python/trunk/Modules/mmapmodule.c

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Mon Jun 29 15:25:16 2009
@@ -12,6 +12,8 @@
 Core and Builtins
 -----------------
 
+- Issue #6344: Fixed a crash of mmap.read() when passed a negative argument.
+
 - Issue #4856: Remove checks for win NT.
 
 - Issue #2016: Fixed a crash in a corner case where the dictionary of keyword

Modified: python/trunk/Modules/mmapmodule.c
==============================================================================
--- python/trunk/Modules/mmapmodule.c	(original)
+++ python/trunk/Modules/mmapmodule.c	Mon Jun 29 15:25:16 2009
@@ -232,7 +232,7 @@
 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 @@
 		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;


More information about the Python-checkins mailing list