]> granicus.if.org Git - python/commitdiff
Issue #8651: PyArg_Parse*() functions raise an OverflowError if the file
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 3 May 2011 13:09:24 +0000 (15:09 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 3 May 2011 13:09:24 +0000 (15:09 +0200)
doesn't have PY_SSIZE_T_CLEAN define and the size doesn't fit in an int (length
bigger than 2^31-1 bytes).

Lib/test/test_xml_etree_c.py
Misc/NEWS
Python/getargs.c

index 78a6a9241c9b97c50f8af7357975ec381bb3e99d..474a4b4e1822655ad2dfdf4dcb2d3e370eb46b1d 100644 (file)
@@ -1,6 +1,8 @@
 # xml.etree test for cElementTree
 
 from test import test_support
+from test.test_support import precisionbigmemtest, _2G
+import unittest
 
 cET = test_support.import_module('xml.etree.cElementTree')
 
@@ -15,6 +17,20 @@ def sanity():
     """
 
 
+class MiscTests(unittest.TestCase):
+    # Issue #8651.
+    @precisionbigmemtest(size=_2G + 100, memuse=1)
+    def test_length_overflow(self, size):
+        if size < _2G + 100:
+            self.skipTest("not enough free memory, need at least 2 GB")
+        data = b'x' * size
+        parser = cET.XMLParser()
+        try:
+            self.assertRaises(OverflowError, parser.feed, data)
+        finally:
+            data = None
+
+
 def test_main():
     from test import test_xml_etree, test_xml_etree_c
 
index e26b6ca316d71070754bb3896666f5b849656938..b14a6cb627872c948156091a19efd0307abc4638 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,10 @@ What's New in Python 2.7.2?
 Core and Builtins
 -----------------
 
+- Issue #8651: PyArg_Parse*() functions raise an OverflowError if the file
+  doesn't have PY_SSIZE_T_CLEAN define and the size doesn't fit in an int
+  (length bigger than 2^31-1 bytes).
+
 - Issue #8651: Fix "z#" format of PyArg_Parse*() function: the size was not
   written if PY_SSIZE_T_CLEAN is defined.
 
index 02351ed6fa0f8dcd73a0fb71e4079a7b5a599458..eccdc9bfb30538d29bd65349f4e548360c0e40bf 100644 (file)
@@ -585,7 +585,17 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
 #define FETCH_SIZE      int *q=NULL;Py_ssize_t *q2=NULL;\
     if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
     else q=va_arg(*p_va, int*);
-#define STORE_SIZE(s)   if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
+#define STORE_SIZE(s)   \
+    if (flags & FLAG_SIZE_T) \
+        *q2=s; \
+    else { \
+        if (INT_MAX < s) { \
+            PyErr_SetString(PyExc_OverflowError, \
+                "size does not fit in an int"); \
+            return converterr("", arg, msgbuf, bufsize); \
+        } \
+        *q=s; \
+    }
 #define BUFFER_LEN      ((flags & FLAG_SIZE_T) ? *q2:*q)
 
     const char *format = *p_format;