diff --git a/src/vm/wren_compiler.c b/src/vm/wren_compiler.c index b29c95cf..19dfe133 100644 --- a/src/vm/wren_compiler.c +++ b/src/vm/wren_compiler.c @@ -2903,6 +2903,16 @@ static void forStatement(Compiler* compiler) // The space in the variable name ensures it won't collide with a user-defined // variable. expression(compiler); + + // Verify that there is space to hidden local variables. + // Note that we expect only two addLocal calls next to each other in the + // following code. + if (compiler->numLocals + 2 > MAX_LOCALS) + { + error(compiler, "Cannot declare more than %d variables in one scope. (Not enough space for for-loops internal variables)", + MAX_LOCALS); + return; + } int seqSlot = addLocal(compiler, "seq ", 4); // Create another hidden local for the iterator object. diff --git a/test/regression/561.wren b/test/regression/561.wren new file mode 100644 index 00000000..8e8fc038 --- /dev/null +++ b/test/regression/561.wren @@ -0,0 +1,178 @@ +// This test caused an array oveflow in local variable array due to hidden +// variables of for loop + +// expect error line 91 +var z = [] +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { +for (x in z) { + +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +} +}