~eliasnaur/gio

80196f3c3ed34140a4fe68fd682c8be21806b37c — Elias Naur 2 years ago 24eb1a4
op: tolerate incomplete macros

Before this change, a macro not Stop'ed would result in an endless
loop during op decoding.

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

M internal/ops/reader.go
M op/op_test.go
M internal/ops/reader.go => internal/ops/reader.go +7 -1
@@ 150,7 150,13 @@ func (r *Reader) Decode() (EncodedOp, bool) {
		case TypeMacro:
			var op opMacroDef
			op.decode(data)
			r.pc = op.endpc
			if op.endpc != (PC{}) {
				r.pc = op.endpc
			} else {
				// Treat an incomplete macro as containing all remaining ops.
				r.pc.data = len(r.ops.data)
				r.pc.refs = len(r.ops.refs)
			}
			continue
		}
		r.pc.data += n

M op/op_test.go => op/op_test.go +16 -0
@@ 5,6 5,8 @@ package op
import (
	"image"
	"testing"

	"gioui.org/internal/ops"
)

func TestTransformChecks(t *testing.T) {


@@ 18,3 20,17 @@ func TestTransformChecks(t *testing.T) {
	Record(&ops)
	trans.Pop()
}

func TestIncompleteMacroReader(t *testing.T) {
	var o Ops
	// Record, but don't Stop it.
	Record(&o)
	Offset(image.Point{}).Push(&o)

	var r ops.Reader

	r.Reset(&o.Internal)
	if _, more := r.Decode(); more {
		t.Error("decoded an operation from a semantically empty Ops")
	}
}