Skip to content

Commit

Permalink
Fix failing assertion with backslash at EOF in macro arguments (gbdev…
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
Rangi42 authored Jan 29, 2025
1 parent a59867c commit c19ddc8
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/asm/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions test/asm/command-line-symbols.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
assert !strcmp("{FOO}", "hello")
assert !strcmp("{DEFINED}", "1")
def FOO equ 42
3 changes: 3 additions & 0 deletions test/asm/command-line-symbols.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
error: command-line-symbols.asm(3):
'FOO' already defined at <command-line>
error: Assembly aborted (1 error)!
1 change: 1 addition & 0 deletions test/asm/command-line-symbols.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Weverything -DFOO=hello -DDEFINED
3 changes: 3 additions & 0 deletions test/asm/for.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
8 changes: 5 additions & 3 deletions test/asm/for.err
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions test/asm/macro-arg-escape-chars.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MACRO mac
DEF s EQUS "\#"
println "{#s:s}"
ENDM
mac \\\"\t\r\0\n\{\}\,\(\)\w\
5 changes: 5 additions & 0 deletions test/asm/macro-arg-escape-chars.err
Original file line number Diff line number Diff line change
@@ -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)!
1 change: 1 addition & 0 deletions test/asm/macro-arg-escape-chars.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\\\"\t\r\0\n\{},()w\\
11 changes: 11 additions & 0 deletions test/asm/string-literal-macro-arg.asm
Original file line number Diff line number Diff line change
@@ -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 "\
9 changes: 9 additions & 0 deletions test/asm/string-literal-macro-arg.err
Original file line number Diff line number Diff line change
@@ -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)!
Binary file added test/asm/string-literal-macro-arg.out
Binary file not shown.

0 comments on commit c19ddc8

Please sign in to comment.