From 492e1474414ba4358fb7ebc35277de50fd7ead31 Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Sun, 19 Dec 2021 00:24:13 +0100 Subject: [PATCH] proper error on unfinished hex escape at end of string. closes #729 --- .../camelot64/kickc/model/values/StringEncoding.java | 2 ++ .../dk/camelot64/kickc/test/TestProgramsFast.java | 10 ++++++++++ src/test/kc/string-escapes-err-2.c | 11 +++++++++++ src/test/kc/string-escapes-err-3.c | 11 +++++++++++ 4 files changed, 34 insertions(+) create mode 100644 src/test/kc/string-escapes-err-2.c create mode 100644 src/test/kc/string-escapes-err-3.c diff --git a/src/main/java/dk/camelot64/kickc/model/values/StringEncoding.java b/src/main/java/dk/camelot64/kickc/model/values/StringEncoding.java index 4382d2c78..b9de85ef5 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/StringEncoding.java +++ b/src/main/java/dk/camelot64/kickc/model/values/StringEncoding.java @@ -173,7 +173,9 @@ public enum StringEncoding { return '\\'; case 'x': String hexNum = ""; + if(escapedCharsIterator.isEmpty()) throw new CompileError("Unfinished string escape sequence at end of string"); hexNum += (char) escapedCharsIterator.pop().intValue(); + if(escapedCharsIterator.isEmpty()) throw new CompileError("Unfinished string escape sequence at end of string"); hexNum += (char) escapedCharsIterator.pop().intValue(); final byte hexEncoding = (byte) Integer.parseInt(hexNum, 16); return charFromEncoded(hexEncoding); diff --git a/src/test/java/dk/camelot64/kickc/test/TestProgramsFast.java b/src/test/java/dk/camelot64/kickc/test/TestProgramsFast.java index 1868bcc90..2e7a66294 100644 --- a/src/test/java/dk/camelot64/kickc/test/TestProgramsFast.java +++ b/src/test/java/dk/camelot64/kickc/test/TestProgramsFast.java @@ -1680,6 +1680,16 @@ public class TestProgramsFast extends TestPrograms { compileAndCompare("code-after-return.c"); } + @Test + public void testStringEscapesErr3() throws IOException { + assertError("string-escapes-err-3.c", "Unfinished string escape sequence at end of string"); + } + + @Test + public void testStringEscapesErr2() throws IOException { + assertError("string-escapes-err-2.c", "Unfinished string escape sequence at end of string"); + } + @Test public void testStringEscapesErr1() throws IOException { assertError("string-escapes-err-1.c", "Illegal string escape sequence"); diff --git a/src/test/kc/string-escapes-err-2.c b/src/test/kc/string-escapes-err-2.c new file mode 100644 index 000000000..0095a4909 --- /dev/null +++ b/src/test/kc/string-escapes-err-2.c @@ -0,0 +1,11 @@ +// Test errors using string escape sequences +// Half hex-escape + +char MESSAGE[] = "qwe\xd"; +char* SCREEN = (char*)0x0400; + +void main() { + byte i=0; + while(MESSAGE[i]) + SCREEN[i] = MESSAGE[i++]; +} \ No newline at end of file diff --git a/src/test/kc/string-escapes-err-3.c b/src/test/kc/string-escapes-err-3.c new file mode 100644 index 000000000..fd0996ece --- /dev/null +++ b/src/test/kc/string-escapes-err-3.c @@ -0,0 +1,11 @@ +// Test errors using string escape sequences +// Half hex-escape + +char MESSAGE[] = "qwe\x"; +char* SCREEN = (char*)0x0400; + +void main() { + byte i=0; + while(MESSAGE[i]) + SCREEN[i] = MESSAGE[i++]; +} \ No newline at end of file