diff --git a/terms.go b/terms.go index 2595f90..91dd360 100644 --- a/terms.go +++ b/terms.go @@ -145,6 +145,11 @@ func parseTerm(desc string, fp *Failpoint) *term { modStr, mods := parseMod(desc) t.mods = &modList{mods} actStr, act := parseAct(desc[len(modStr):]) + if act == nil { + // A term without an action has nothing to run, and calling its + // nil act func later panics. + return nil + } t.act = act valStr, val := parseVal(desc[len(modStr)+len(actStr):]) t.val = val diff --git a/terms_test.go b/terms_test.go index d1b302f..79cda29 100644 --- a/terms_test.go +++ b/terms_test.go @@ -92,3 +92,30 @@ func TestTermsTypes(t *testing.T) { } } } + +func TestTermsRejectsMissingAction(t *testing.T) { + const fpname = "github.com/pingcap/failpoint/missingAction" + // A term is " [ ( ) ]". With the action left out there + // is nothing to run, and the chain used to be accepted and then call a + // nil act func on the first evaluation. + for _, desc := range []string{`1*`, `100%`, `1*2*`, `100.0%`} { + if _, err := newTerms(desc, nil); err == nil { + t.Errorf("newTerms(%q) was accepted, want a parse error", desc) + } + if err := Enable(fpname, desc); err != nil { + continue + } + func() { + defer func() { + if r := recover(); r != nil { + t.Errorf("Enable(%q) was accepted and the failpoint then panicked: %v", desc, r) + } + }() + _, _ = Eval(fpname) + t.Errorf("Enable(%q) was accepted, want a parse error", desc) + }() + if err := Disable(fpname); err != nil { + t.Fatal(err) + } + } +}