From deef67481c37965474b64c7e703982998c20a654 Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Fri, 14 Mar 2008 17:16:59 +0000 Subject: Fix crasher in unpacking assignments with star, where the size constraints weren't checked. --- Lib/test/test_unpack_ex.py | 17 +++++++++++++++++ Python/compile.c | 10 ++++++++++ 2 files changed, 27 insertions(+) diff --git a/Lib/test/test_unpack_ex.py b/Lib/test/test_unpack_ex.py index 7fe486b..89486b8 100644 --- a/Lib/test/test_unpack_ex.py +++ b/Lib/test/test_unpack_ex.py @@ -143,6 +143,23 @@ Now some general starred expressions (all fail). ... 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} diff --git a/Python/compile.c b/Python/compile.c index af6e067..f415519 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2614,6 +2614,11 @@ compiler_list(struct compiler *c, expr_ty e) 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 @@ compiler_tuple(struct compiler *c, expr_ty e) 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); -- cgit v0.12