Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 451385d

Browse files
author
Victor Stinner
committed
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).
1 parent e788ace commit 451385d

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

Lib/test/test_xml_etree_c.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import sys
55

66
from test import support
7+
from test.support import precisionbigmemtest, _2G
8+
import unittest
79

810
ET = support.import_module('xml.etree.cElementTree')
911

@@ -212,9 +214,25 @@ def bug_1534630():
212214
'<tag />'
213215
"""
214216

217+
class MiscTests(unittest.TestCase):
218+
# Issue #8651.
219+
@support.precisionbigmemtest(size=support._2G + 100, memuse=1)
220+
def test_length_overflow(self, size):
221+
if size < support._2G + 100:
222+
self.skipTest("not enough free memory, need at least 2 GB")
223+
data = b'x' * size
224+
parser = ET.XMLParser()
225+
try:
226+
self.assertRaises(OverflowError, parser.feed, data)
227+
finally:
228+
data = None
229+
230+
215231
def test_main():
216232
from test import test_xml_etree_c
217233
support.run_doctest(test_xml_etree_c, verbosity=True)
218234

235+
support.run_unittest(MiscTests)
236+
219237
if __name__ == '__main__':
220238
test_main()

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.1.4?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #8651: PyArg_Parse*() functions raise an OverflowError if the file
14+
doesn't have PY_SSIZE_T_CLEAN define and the size doesn't fit in an int
15+
(length bigger than 2^31-1 bytes).
16+
1317
- Issue #11450: Don't truncate hg version info in Py_GetBuildInfo() when
1418
there are many tags (e.g. when using mq). Patch by Nadeem Vawda.
1519

Python/getargs.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,17 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
613613
#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
614614
if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
615615
else q=va_arg(*p_va, int*);
616-
#define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
616+
#define STORE_SIZE(s) \
617+
if (flags & FLAG_SIZE_T) \
618+
*q2=s; \
619+
else { \
620+
if (INT_MAX < s) { \
621+
PyErr_SetString(PyExc_OverflowError, \
622+
"size does not fit in an int"); \
623+
return converterr("", arg, msgbuf, bufsize); \
624+
} \
625+
*q=s; \
626+
}
617627
#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
618628

619629
const char *format = *p_format;

0 commit comments

Comments
 (0)