Skip to content

Commit

Permalink
Fix infinite loop in AST prune (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristonianJones authored Mar 25, 2021
1 parent 066a51c commit f86a886
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
9 changes: 4 additions & 5 deletions interpreter/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,11 @@ func (p *astPruner) existsWithKnownValue(id int64) bool {
func (p *astPruner) nextID() int64 {
for {
_, found := p.state.Value(p.nextExprID)
if found {
break
if !found {
next := p.nextExprID
p.nextExprID++
return next
}
p.nextExprID++
}
next := p.nextExprID
p.nextExprID++
return next
}
24 changes: 22 additions & 2 deletions interpreter/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ import (
)

type testInfo struct {
in Activation
in interface{}
expr string
out string
}

var testCases = []testInfo{
{
in: map[string]interface{}{
"msg": map[string]string{"foo": "bar"},
},
expr: `msg`,
out: `{"foo": "bar"}`,
},
{
expr: `true && false`,
out: `false`,
Expand Down Expand Up @@ -128,10 +135,11 @@ func TestPrune(t *testing.T) {
reg := newTestRegistry(t)
attrs := NewPartialAttributeFactory(containers.DefaultContainer, reg, reg)
interp := NewStandardInterpreter(containers.DefaultContainer, reg, reg, attrs)

interpretable, _ := interp.NewUncheckedInterpretable(
ast.Expr,
ExhaustiveEval(state))
interpretable.Eval(tst.in)
interpretable.Eval(testActivation(t, tst.in))
newExpr := PruneAst(ast.Expr, state)
actual, err := parser.Unparse(newExpr, nil)
if err != nil {
Expand All @@ -151,3 +159,15 @@ func unknownActivation(vars ...string) PartialActivation {
a, _ := NewPartialActivation(map[string]interface{}{}, pats...)
return a
}

func testActivation(t *testing.T, in interface{}) Activation {
t.Helper()
if in == nil {
return EmptyActivation()
}
a, err := NewActivation(in)
if err != nil {
t.Fatalf("NewActivation(%v) failed: %v", in, err)
}
return a
}

0 comments on commit f86a886

Please sign in to comment.