From 29e48f3cac1dac9bff01b52016c4e15888d55ce5 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sat, 6 Sep 2025 22:27:45 -0400 Subject: [PATCH] [script] Fix varible inline instructions; Fix off-by-one - Fixed instructions that have -3 as the OpCount (count then count words). There is not an extra word that acts as the default selection. These instructions do nothing if the argument is out of range. - Fixed off-by-one eating the byte following the -3 OpCount instructions. - Fixed panic when a -2 op code goes beyond the end of the script. --- script/parser.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/script/parser.go b/script/parser.go index c223dc6..e491ff9 100644 --- a/script/parser.go +++ b/script/parser.go @@ -45,7 +45,7 @@ func Parse(rawinput []byte, startAddr int) (*Script, error) { op, ok := InstrMap[raw] if !ok { - return nil, fmt.Errorf("OP %02X not in instruction map", raw) + return nil, fmt.Errorf("OP 0x%02X not in instruction map", raw) } token.Instruction = op @@ -66,19 +66,25 @@ func Parse(rawinput []byte, startAddr int) (*Script, error) { args = append(args, ByteVal(l)) i++ for c := 0; c < l; c++ { + if len(rawinput) <= i+1 { + return script, fmt.Errorf("OP early end at offset 0x%X (%d) {%d} %#v", i, i, l, op) + } + args = append(args, WordVal([2]byte{rawinput[i], rawinput[i+1]})) i+=2 } + i-- - case -3: // count then count+1 words (extra is default case) + case -3: // count then count words. "default" is no call (skip Code_Pointer to after args) i++ l := int(rawinput[i]) args = append(args, ByteVal(l)) i++ - for c := 0; c < l+1; c++ { + for c := 0; c < l; c++ { args = append(args, WordVal([2]byte{rawinput[i], rawinput[i+1]})) i+=2 } + i-- case 2: args = append(args, WordVal([2]byte{rawinput[i+1], rawinput[i+2]})) @@ -92,6 +98,7 @@ func Parse(rawinput []byte, startAddr int) (*Script, error) { token.Inline = args } + // Find and mark labels for a few instructions for _, t := range script.Tokens { switch t.Raw { case 0x84, 0x85, 0xBF, 0xC0: // jmp/call