Skip to content

Commit c19ddc8

Browse files
authored
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.
1 parent a59867c commit c19ddc8

12 files changed

+49
-5
lines changed

src/asm/lexer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,9 @@ void BufferedContent::advance() {
509509
if (offset == std::size(buf)) {
510510
offset = 0; // Wrap around if necessary
511511
}
512-
assume(size > 0);
513-
size--;
512+
if (size > 0) {
513+
size--;
514+
}
514515
}
515516

516517
void BufferedContent::refill() {

test/asm/command-line-symbols.asm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
assert !strcmp("{FOO}", "hello")
2+
assert !strcmp("{DEFINED}", "1")
3+
def FOO equ 42

test/asm/command-line-symbols.err

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
error: command-line-symbols.asm(3):
2+
'FOO' already defined at <command-line>
3+
error: Assembly aborted (1 error)!

test/asm/command-line-symbols.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Weverything -DFOO=hello -DDEFINED

test/asm/for.asm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ for {s}, 3, 30, 3
4141
print "{d:x} "
4242
endr
4343
println "-> {d:x}"
44+
for s, 10
45+
println "{d:s}"
46+
endr
4447

4548
for v, 10
4649
println "{d:v}"

test/asm/for.err

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ error: for.asm(16):
44
FOR cannot have a step value of 0
55
warning: for.asm(20): [-Wbackwards-for]
66
FOR goes backwards from 1 to 2 by -1
7-
error: for.asm(45) -> for.asm::REPT~4(51):
8-
'v' already defined as constant at for.asm(45) -> for.asm::REPT~4(49)
9-
FATAL: for.asm(45) -> for.asm::REPT~4(51):
7+
error: for.asm(46):
8+
's' already defined as constant at for.asm(39)
9+
error: for.asm(48) -> for.asm::REPT~4(54):
10+
'v' already defined as constant at for.asm(48) -> for.asm::REPT~4(52)
11+
FATAL: for.asm(48) -> for.asm::REPT~4(54):
1012
Failed to update FOR symbol value

test/asm/macro-arg-escape-chars.asm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
MACRO mac
2+
DEF s EQUS "\#"
3+
println "{#s:s}"
4+
ENDM
5+
mac \\\"\t\r\0\n\{\}\,\(\)\w\

test/asm/macro-arg-escape-chars.err

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: macro-arg-escape-chars.asm(5):
2+
Illegal character escape 'w'
3+
error: macro-arg-escape-chars.asm(5):
4+
Illegal character escape at end of input
5+
error: Assembly aborted (2 errors)!

test/asm/macro-arg-escape-chars.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
\\\"\t\r\0\n\{},()w\\

test/asm/string-literal-macro-arg.asm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
MACRO mac
2+
println "\1"
3+
println \1
4+
ENDM
5+
mac "hello \\\"\t\r\0\n\ ; comment
6+
\wor\
7+
ld"
8+
mac """goodbye
9+
cruel\ ; comment
10+
\nworld"""
11+
mac "\

test/asm/string-literal-macro-arg.err

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error: string-literal-macro-arg.asm(6):
2+
Illegal character escape 'w'
3+
error: string-literal-macro-arg.asm(11):
4+
Illegal character escape at end of input
5+
error: string-literal-macro-arg.asm(11):
6+
Unterminated string
7+
error: string-literal-macro-arg.asm(11) -> string-literal-macro-arg.asm::mac(4):
8+
Unterminated string
9+
error: Assembly aborted (4 errors)!

test/asm/string-literal-macro-arg.out

97 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)