Skip to content

Commit 64ebdf3

Browse files
committed
Check overflow errors in the parsing bytecode buffer.
Don't simply emit bad code on lines with too long bytecode.
1 parent 584bd2f commit 64ebdf3

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

src/actions.asm

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
.import alloc_laddr
4242
.importzp prog_ptr, laddr_ptr, laddr_buf, var_ptr, label_ptr
4343
; From parser.asm
44-
.import parser_error, parser_skipws
44+
.import parser_error, parser_skipws, parser_emit_byte
4545
.importzp TOK_CSTRING
4646
; From error.asm
4747
.importzp ERR_LOOP
@@ -141,15 +141,10 @@ ok: rts
141141
;
142142
; Emits 16bit AX into codep
143143
.proc emit_AX
144-
ldy opos
145-
new_y: sta (prog_ptr),y
146-
txa
147-
iny
148-
sta (prog_ptr),y
149-
iny
150-
sty opos
151144
clc
152-
rts
145+
jsr parser_emit_byte
146+
txa
147+
jmp parser_emit_byte
153148
.endproc
154149

155150
; Parser external subs
@@ -286,9 +281,7 @@ nloop:
286281
; Store
287282
store: inx
288283
inc bpos
289-
ldy opos
290-
sta (prog_ptr),y
291-
inc opos
284+
jsr parser_emit_byte
292285
bne nloop
293286
err: ; Restore opos and exit
294287
lda tmp1
@@ -379,9 +372,7 @@ exit:
379372
.proc emit_varn
380373
; Store VARN
381374
txa
382-
ldy opos
383-
sta (prog_ptr),y
384-
inc opos
375+
jsr parser_emit_byte
385376
; Fall through
386377
.endproc
387378
; Advances variable name in source pointer

src/errors.asm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ error_msg_list:
3434
.endrepeat
3535
.byte .strat(msg, .strlen(msg)-1) ^ $80
3636
.endmacro
37-
def_error ERR_LOOP, "bad loop error"
37+
def_error ERR_TOO_LONG, "too long"
38+
def_error ERR_LOOP, "bad loop"
3839
def_error ERR_PARSE, "parse error"
3940
def_error ERR_NO_ELOOP, "no end loop/proc/if"
4041
def_error ERR_LABEL, "undef label"

src/parse.asm

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
; Parser state machine interpreter
2020
; --------------------------------
2121

22-
.export parser_start, parser_error, parser_skipws
22+
.export parser_start, parser_error, parser_skipws, parser_emit_byte
2323
; Parser state
2424
.exportzp bptr, bpos, bmax, linenum, buf_ptr, end_ptr
2525
.exportzp loop_sp
@@ -42,7 +42,7 @@
4242
.importzp DEGFLAG, DEGFLAG_DEG, DEGFLAG_RAD
4343
; From errors.asm
4444
.import error_msg_list
45-
.importzp ERR_PARSE, ERR_NO_ELOOP, ERR_LABEL
45+
.importzp ERR_PARSE, ERR_NO_ELOOP, ERR_LABEL, ERR_TOO_LONG
4646

4747
.zeropage
4848
buf_ptr:.res 2
@@ -96,6 +96,17 @@ SM_EMIT= SM_EMIT_1
9696
; Now, the rest of the code
9797
.code
9898

99+
emit_sub:
100+
jsr parser_fetch
101+
emit_const:
102+
parser_emit_byte:
103+
ldy opos
104+
sta (prog_ptr),y
105+
inc opos
106+
bne rts1
107+
lda #ERR_TOO_LONG
108+
; Fall through
109+
99110
.proc parser_error
100111
; Prints error message
101112
tax
@@ -116,7 +127,7 @@ skip: bpl ploop
116127
::restore_stack:
117128
ldx #$ff
118129
txs
119-
rts
130+
::rts1: rts
120131
.endproc
121132
saved_stack = restore_stack + 1
122133

@@ -393,12 +404,4 @@ set_parse_error:
393404
lda #ERR_PARSE
394405
jmp parser_error
395406

396-
emit_sub:
397-
jsr parser_fetch
398-
emit_const:
399-
ldy opos
400-
sta (prog_ptr),y
401-
inc opos
402-
rts
403-
404407
; vi:syntax=asm_ca65

0 commit comments

Comments
 (0)