From c19ddc80f0df35398c4777f298bef18cb99aaf93 Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Tue, 28 Jan 2025 21:51:11 -0500 Subject: [PATCH] Fix failing assertion with backslash at EOF in macro arguments (#1634) `Expansion::advance()` can increase its offset beyond the size, so I don't think this assumption was valid in the first place; `BufferedContent::advance()` should be able to as well. --- src/asm/lexer.cpp | 5 +++-- test/asm/command-line-symbols.asm | 3 +++ test/asm/command-line-symbols.err | 3 +++ test/asm/command-line-symbols.flags | 1 + test/asm/for.asm | 3 +++ test/asm/for.err | 8 +++++--- test/asm/macro-arg-escape-chars.asm | 5 +++++ test/asm/macro-arg-escape-chars.err | 5 +++++ test/asm/macro-arg-escape-chars.out | 1 + test/asm/string-literal-macro-arg.asm | 11 +++++++++++ test/asm/string-literal-macro-arg.err | 9 +++++++++ test/asm/string-literal-macro-arg.out | Bin 0 -> 97 bytes 12 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 test/asm/command-line-symbols.asm create mode 100644 test/asm/command-line-symbols.err create mode 100644 test/asm/command-line-symbols.flags create mode 100644 test/asm/macro-arg-escape-chars.asm create mode 100644 test/asm/macro-arg-escape-chars.err create mode 100644 test/asm/macro-arg-escape-chars.out create mode 100644 test/asm/string-literal-macro-arg.asm create mode 100644 test/asm/string-literal-macro-arg.err create mode 100644 test/asm/string-literal-macro-arg.out diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 0911273ad..3027baf7d 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -509,8 +509,9 @@ void BufferedContent::advance() { if (offset == std::size(buf)) { offset = 0; // Wrap around if necessary } - assume(size > 0); - size--; + if (size > 0) { + size--; + } } void BufferedContent::refill() { diff --git a/test/asm/command-line-symbols.asm b/test/asm/command-line-symbols.asm new file mode 100644 index 000000000..109bcfb1f --- /dev/null +++ b/test/asm/command-line-symbols.asm @@ -0,0 +1,3 @@ +assert !strcmp("{FOO}", "hello") +assert !strcmp("{DEFINED}", "1") +def FOO equ 42 diff --git a/test/asm/command-line-symbols.err b/test/asm/command-line-symbols.err new file mode 100644 index 000000000..34337f520 --- /dev/null +++ b/test/asm/command-line-symbols.err @@ -0,0 +1,3 @@ +error: command-line-symbols.asm(3): + 'FOO' already defined at +error: Assembly aborted (1 error)! diff --git a/test/asm/command-line-symbols.flags b/test/asm/command-line-symbols.flags new file mode 100644 index 000000000..4fcbb175b --- /dev/null +++ b/test/asm/command-line-symbols.flags @@ -0,0 +1 @@ +-Weverything -DFOO=hello -DDEFINED diff --git a/test/asm/for.asm b/test/asm/for.asm index 72102671b..786b3bbba 100644 --- a/test/asm/for.asm +++ b/test/asm/for.asm @@ -41,6 +41,9 @@ for {s}, 3, 30, 3 print "{d:x} " endr println "-> {d:x}" +for s, 10 + println "{d:s}" +endr for v, 10 println "{d:v}" diff --git a/test/asm/for.err b/test/asm/for.err index 6e9cc7bdb..0574afaf8 100644 --- a/test/asm/for.err +++ b/test/asm/for.err @@ -4,7 +4,9 @@ error: for.asm(16): FOR cannot have a step value of 0 warning: for.asm(20): [-Wbackwards-for] FOR goes backwards from 1 to 2 by -1 -error: for.asm(45) -> for.asm::REPT~4(51): - 'v' already defined as constant at for.asm(45) -> for.asm::REPT~4(49) -FATAL: for.asm(45) -> for.asm::REPT~4(51): +error: for.asm(46): + 's' already defined as constant at for.asm(39) +error: for.asm(48) -> for.asm::REPT~4(54): + 'v' already defined as constant at for.asm(48) -> for.asm::REPT~4(52) +FATAL: for.asm(48) -> for.asm::REPT~4(54): Failed to update FOR symbol value diff --git a/test/asm/macro-arg-escape-chars.asm b/test/asm/macro-arg-escape-chars.asm new file mode 100644 index 000000000..08e2eb123 --- /dev/null +++ b/test/asm/macro-arg-escape-chars.asm @@ -0,0 +1,5 @@ +MACRO mac + DEF s EQUS "\#" + println "{#s:s}" +ENDM +mac \\\"\t\r\0\n\{\}\,\(\)\w\ \ No newline at end of file diff --git a/test/asm/macro-arg-escape-chars.err b/test/asm/macro-arg-escape-chars.err new file mode 100644 index 000000000..06d9d2a63 --- /dev/null +++ b/test/asm/macro-arg-escape-chars.err @@ -0,0 +1,5 @@ +error: macro-arg-escape-chars.asm(5): + Illegal character escape 'w' +error: macro-arg-escape-chars.asm(5): + Illegal character escape at end of input +error: Assembly aborted (2 errors)! diff --git a/test/asm/macro-arg-escape-chars.out b/test/asm/macro-arg-escape-chars.out new file mode 100644 index 000000000..84b5a2636 --- /dev/null +++ b/test/asm/macro-arg-escape-chars.out @@ -0,0 +1 @@ +\\\"\t\r\0\n\{},()w\\ diff --git a/test/asm/string-literal-macro-arg.asm b/test/asm/string-literal-macro-arg.asm new file mode 100644 index 000000000..50d4114b5 --- /dev/null +++ b/test/asm/string-literal-macro-arg.asm @@ -0,0 +1,11 @@ +MACRO mac + println "\1" + println \1 +ENDM +mac "hello \\\"\t\r\0\n\ ; comment + \wor\ +ld" +mac """goodbye +cruel\ ; comment +\nworld""" +mac "\ \ No newline at end of file diff --git a/test/asm/string-literal-macro-arg.err b/test/asm/string-literal-macro-arg.err new file mode 100644 index 000000000..0066f05ff --- /dev/null +++ b/test/asm/string-literal-macro-arg.err @@ -0,0 +1,9 @@ +error: string-literal-macro-arg.asm(6): + Illegal character escape 'w' +error: string-literal-macro-arg.asm(11): + Illegal character escape at end of input +error: string-literal-macro-arg.asm(11): + Unterminated string +error: string-literal-macro-arg.asm(11) -> string-literal-macro-arg.asm::mac(4): + Unterminated string +error: Assembly aborted (4 errors)! diff --git a/test/asm/string-literal-macro-arg.out b/test/asm/string-literal-macro-arg.out new file mode 100644 index 0000000000000000000000000000000000000000..8fea78e8beb56b1700015b73d75919fb9d6f4add GIT binary patch literal 97 zcmY+)u?c`c5JXW^2khaP3M^