[Python-checkins] cpython (2.7): Issue #14888: Fix misbehaviour of the _md5 module when called on data larger

antoine.pitrou python-checkins at python.org
Wed May 23 23:19:20 CEST 2012


http://hg.python.org/cpython/rev/290d970c011d
changeset:   77118:290d970c011d
branch:      2.7
parent:      77105:086afe7b61f5
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Wed May 23 23:16:14 2012 +0200
summary:
  Issue #14888: Fix misbehaviour of the _md5 module when called on data larger than 2**32 bytes.

files:
  Misc/NEWS           |   3 +++
  Modules/md5module.c |  17 ++++++++++++++---
  2 files changed, 17 insertions(+), 3 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -64,6 +64,9 @@
 Library
 -------
 
+- Issue #14888: Fix misbehaviour of the _md5 module when called on data
+  larger than 2**32 bytes.
+
 - Issue #14875: Use float('inf') instead of float('1e66666') in the json module.
 
 - Issue #14572: Prevent build failures with pre-3.5.0 versions of
diff --git a/Modules/md5module.c b/Modules/md5module.c
--- a/Modules/md5module.c
+++ b/Modules/md5module.c
@@ -262,6 +262,8 @@
 {
     md5object *md5p;
     Py_buffer view = { 0 };
+    Py_ssize_t n;
+    unsigned char *buf;
 
     if (!PyArg_ParseTuple(args, "|s*:new", &view))
         return NULL;
@@ -271,9 +273,18 @@
         return NULL;
     }
 
-    if (view.len > 0) {
-        md5_append(&md5p->md5, (unsigned char*)view.buf,
-               Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
+    n = view.len;
+    buf = (unsigned char *) view.buf;
+    while (n > 0) {
+        Py_ssize_t nbytes;
+        if (n > INT_MAX)
+            nbytes = INT_MAX;
+        else
+            nbytes = n;
+        md5_append(&md5p->md5, buf,
+                   Py_SAFE_DOWNCAST(nbytes, Py_ssize_t, unsigned int));
+        buf += nbytes;
+        n -= nbytes;
     }
     PyBuffer_Release(&view);
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list