summaryrefslogtreecommitdiffstatshomepage
path: root/py/compile.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-04-05 10:50:26 +1000
committerDamien George <damien.p.george@gmail.com>2017-04-05 10:50:26 +1000
commitde9b53695d1bf8d7580e8371e4bc502a29334e6f (patch)
treeb2a713516bad10df36487498fdfdf88988a1cb85 /py/compile.c
parent546ef301a12ccd6015137964637983432c64d11f (diff)
downloadmicropython-de9b53695d1bf8d7580e8371e4bc502a29334e6f.tar.gz
micropython-de9b53695d1bf8d7580e8371e4bc502a29334e6f.zip
py: Raise a ValueError if range() step is zero.
Following CPython. Otherwise one gets either an infinite loop (if code is optimised by the uPy compiler) or possibly a divide-by-zero CPU exception.
Diffstat (limited to 'py/compile.c')
-rw-r--r--py/compile.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/py/compile.c b/py/compile.c
index b2811e9585..cf9e5079b0 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -1444,8 +1444,9 @@ STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
pn_range_start = args[0];
pn_range_end = args[1];
pn_range_step = args[2];
- // We need to know sign of step. This is possible only if it's constant
- if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
+ // the step must be a non-zero constant integer to do the optimisation
+ if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)
+ || MP_PARSE_NODE_LEAF_SMALL_INT(pn_range_step) == 0) {
optimize = false;
}
}