diff Modules/zlibmodule.c @ 67343:4549426ee4d6

Issue #8650: zlib.compress() and zlib.decompress() raise an OverflowError if the input buffer length doesn't fit into an unsigned int (length bigger than 2^32-1 bytes).
author Victor Stinner <victor.stinner@haypocalc.com>
date Tue, 04 Jan 2011 02:07:36 +0000
parents 48d9664c47a3
children 51a080bacb25
line wrap: on
line diff
--- a/Modules/zlibmodule.c	Tue Jan 04 02:07:34 2011 +0000
+++ b/Modules/zlibmodule.c	Tue Jan 04 02:07:36 2011 +0000
@@ -117,14 +117,21 @@
     PyObject *ReturnVal = NULL;
     Py_buffer pinput;
     Byte *input, *output;
-    int length, level=Z_DEFAULT_COMPRESSION, err;
+    unsigned int length;
+    int level=Z_DEFAULT_COMPRESSION, err;
     z_stream zst;
 
     /* require Python string object, optional 'level' arg */
     if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
         return NULL;
+
+    if (pinput.len > UINT_MAX) {
+        PyErr_SetString(PyExc_OverflowError,
+            "size does not fit in an unsigned int");
+        return NULL;
+    }
+    length = pinput.len;
     input = pinput.buf;
-    length = pinput.len;
 
     zst.avail_out = length + length/1000 + 12 + 1;
 
@@ -199,7 +206,8 @@
     PyObject *result_str;
     Py_buffer pinput;
     Byte *input;
-    int length, err;
+    unsigned int length;
+    int err;
     int wsize=DEF_WBITS;
     Py_ssize_t r_strlen=DEFAULTALLOC;
     z_stream zst;
@@ -207,8 +215,14 @@
     if (!PyArg_ParseTuple(args, "y*|in:decompress",
                           &pinput, &wsize, &r_strlen))
         return NULL;
+
+    if (pinput.len > UINT_MAX) {
+        PyErr_SetString(PyExc_OverflowError,
+            "size does not fit in an unsigned int");
+        return NULL;
+    }
+    length = pinput.len;
     input = pinput.buf;
-    length = pinput.len;
 
     if (r_strlen <= 0)
         r_strlen = 1;