[Python-3000-checkins] r61386 - in python/branches/py3k: Lib/test/test_unpack_ex.py Python/compile.c

thomas.wouters python-3000-checkins at python.org
Fri Mar 14 18:16:59 CET 2008


Author: thomas.wouters
Date: Fri Mar 14 18:16:59 2008
New Revision: 61386

Modified:
   python/branches/py3k/Lib/test/test_unpack_ex.py
   python/branches/py3k/Python/compile.c
Log:

Fix crasher in unpacking assignments with star, where the size constraints
weren't checked.



Modified: python/branches/py3k/Lib/test/test_unpack_ex.py
==============================================================================
--- python/branches/py3k/Lib/test/test_unpack_ex.py	(original)
+++ python/branches/py3k/Lib/test/test_unpack_ex.py	Fri Mar 14 18:16:59 2008
@@ -143,6 +143,23 @@
       ...
     SyntaxError: can use starred expression only as assignment target
 
+Some size constraints (all fail.)
+
+    >>> s = ", ".join("a%d" % i for i in range(1<<8)) + ", *rest = range(1<<8 + 1)"
+    >>> compile(s, 'test', 'exec') # doctest:+ELLIPSIS
+    Traceback (most recent call last):
+     ...
+    SyntaxError: too many expressions in star-unpacking assignment
+
+    >>> s = ", ".join("a%d" % i for i in range(1<<8 + 1)) + ", *rest = range(1<<8 + 2)"
+    >>> compile(s, 'test', 'exec') # doctest:+ELLIPSIS
+    Traceback (most recent call last):
+     ...
+    SyntaxError: too many expressions in star-unpacking assignment
+
+(there is an additional limit, on the number of expressions after the
+'*rest', but it's 1<<24 and testing it takes too much memory.)
+
 """
 
 __test__ = {'doctests' : doctests}

Modified: python/branches/py3k/Python/compile.c
==============================================================================
--- python/branches/py3k/Python/compile.c	(original)
+++ python/branches/py3k/Python/compile.c	Fri Mar 14 18:16:59 2008
@@ -2614,6 +2614,11 @@
 		for (i = 0; i < n; i++) {
 			expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
 			if (elt->kind == Starred_kind && !seen_star) {
+				if ((i >= (1 << 8)) ||
+				    (n-i-1 >= (INT_MAX >> 8)))
+					return compiler_error(c,
+						"too many expressions in "
+						"star-unpacking assignment");
 				ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
 				seen_star = 1;
 				asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
@@ -2642,6 +2647,11 @@
 		for (i = 0; i < n; i++) {
 			expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
 			if (elt->kind == Starred_kind && !seen_star) {
+				if ((i >= (1 << 8)) ||
+				    (n-i-1 >= (INT_MAX >> 8)))
+					return compiler_error(c,
+						"too many expressions in "
+						"star-unpacking assignment");
 				ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
 				seen_star = 1;
 				asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);


More information about the Python-3000-checkins mailing list