From 765bc2a2697b562f7741175ce871f4f791a05979 Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 10 Apr 2023 09:19:18 +0100 Subject: [PATCH] Terminate on code EOF when not checking input --- go/state/flag.go | 5 +++-- go/vm/runner.go | 43 ++++++++++++++++++++++++++++++++++++------- go/vm/runner_test.go | 2 +- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/go/state/flag.go b/go/state/flag.go index 2634c8e..b829415 100644 --- a/go/state/flag.go +++ b/go/state/flag.go @@ -1,7 +1,8 @@ package state const ( - FLAG_INMATCH = 1 - FLAG_TERMINATE = 2 + FLAG_READIN = 1 + FLAG_INMATCH = 2 + FLAG_TERMINATE = 3 FLAG_DIRTY = 4 ) diff --git a/go/vm/runner.go b/go/vm/runner.go index 4d7f07c..4bb192c 100644 --- a/go/vm/runner.go +++ b/go/vm/runner.go @@ -125,7 +125,19 @@ func(vm *Vm) RunDeadCheck(b []byte, ctx context.Context) ([]byte, error) { if len(b) > 0 { return b, nil } - r, err := vm.st.MatchFlag(state.FLAG_TERMINATE, false) + r, err := vm.st.MatchFlag(state.FLAG_READIN, true) + if err != nil { + panic(err) + } + if r { + log.Printf("Not processing input. Setting terminate") + _, err := vm.st.SetFlag(state.FLAG_TERMINATE) + if err != nil { + panic(err) + } + return b, nil + } + r, err = vm.st.MatchFlag(state.FLAG_TERMINATE, false) if err != nil { panic(err) } @@ -133,6 +145,8 @@ func(vm *Vm) RunDeadCheck(b []byte, ctx context.Context) ([]byte, error) { log.Printf("Terminate found!!") return b, nil } + + log.Printf("no code remaining but not terminating") location, _ := vm.st.Where() if location == "" { @@ -252,12 +266,24 @@ func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) { if err != nil { return b, err } - v, err := vm.st.GetFlag(state.FLAG_INMATCH) + + change, err := vm.st.SetFlag(state.FLAG_READIN) if err != nil { - return b, err + panic(err) } - if v { - return b, nil + have, err := vm.st.GetFlag(state.FLAG_INMATCH) + if err != nil { + panic(err) + } + if have { + if change { + _, err = vm.st.ResetFlag(state.FLAG_INMATCH) + if err != nil { + panic(err) + } + } else { + return b, nil + } } input, err := vm.st.GetInput() if err != nil { @@ -275,7 +301,11 @@ func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) { _, err = vm.st.SetFlag(state.FLAG_INMATCH) if err != nil { - return b, err + panic(err) + } + _, err = vm.st.ResetFlag(state.FLAG_READIN) + if err != nil { + panic(err) } target, _, err = applyTarget([]byte(target), vm.st, vm.ca, ctx) @@ -300,7 +330,6 @@ func(vm *Vm) RunHalt(b []byte, ctx context.Context) ([]byte, error) { return b, err } log.Printf("found HALT, stopping") - _, err = vm.st.ResetFlag(state.FLAG_INMATCH) return b, err } diff --git a/go/vm/runner_test.go b/go/vm/runner_test.go index c4f5591..3fa1ab1 100644 --- a/go/vm/runner_test.go +++ b/go/vm/runner_test.go @@ -396,6 +396,6 @@ func TestRunReturn(t *testing.T) { } location, _ = st.Where() if location != "root" { - t.Fatalf("expected location 'foo', got '%s'", location) + t.Fatalf("expected location 'root', got '%s'", location) } }