93409a784d7c687a9883e2d9bb18c57ce563b3fc — Elias Naur 4 months ago 85843f1
ui: fill out the MacroOp opcode in Record to support unfinished macros

Before this change MacroOp.Record simply reserved enough space for Stop to fill
out. If a user Record but never Stop'ed a MacroOp, the resulting Ops buffer
would end up with a zero, invalid opcode and panic at decode.

Fill out an empty MacroOp at Record, ensuring the Ops remains valid.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
1 files changed, 6 insertions(+), 1 deletions(-)

M ui/ops.go
M ui/ops.go => ui/ops.go +6 -1
@@ 157,8 157,9 @@ func (m *MacroOp) Record(o *Ops) {
 	m.recording = true
 	m.ops = o
 	m.pc = o.pc()
-	// Make room for a macro definition. Filled out in Stop.
+	// Reserve room for a macro definition. Updated in Stop.
 	m.ops.Write(make([]byte, opconst.TypeMacroDefLen))
+	m.fill()
 }
 
 // Stop ends a previously started recording.


@@ 167,6 168,10 @@ func (m *MacroOp) Stop() {
 		panic("not recording")
 	}
 	m.recording = false
+	m.fill()
+}
+
+func (m *MacroOp) fill() {
 	pc := m.ops.pc()
 	// Fill out the macro definition reserved in Record.
 	data := m.ops.data[m.pc.data:]