1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-01-30 11:38:38 +00:00

Fixed NPE if ranged loop-variable is not declared. Closes #333

This commit is contained in:
jespergravgaard 2019-10-03 10:41:10 +02:00
parent f4206a2e84
commit 812546d603
3 changed files with 21 additions and 0 deletions

View File

@ -1207,6 +1207,9 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
addDirectives(lValue, varType, varDirectives, statementSource);
} else {
lValue = getCurrentScope().getVariable(varName);
if(lValue==null) {
throw new CompileError("Error! Loop variable not declared "+varName, statementSource);
}
}
exitDeclTypes();
KickCParser.StmtForContext stmtForCtx = (KickCParser.StmtForContext) ctx.getParent();

View File

@ -2410,6 +2410,11 @@ public class TestPrograms {
compileAndCompare("double-assignment");
}
@Test
public void testLoopNpe() throws IOException, URISyntaxException {
assertError("loop-npe", "Error! Loop variable not declared");
}
@Test
public void testConstWordPointer() throws IOException, URISyntaxException {
compileAndCompare("const-word-pointer");

13
src/test/kc/loop-npe.kc Normal file
View File

@ -0,0 +1,13 @@
// Illustrated compiler NPE when doing ranged loop without declaring the loop variable.
void main() {
byte* SCREEN = 0x0400;
clear_line(SCREEN);
}
void clear_line(byte *line) {
for (i: 0..39)
line[i] = 0;
}