Skip to content

Commit

Permalink
fix: ast left cycular reference result in oom (#13501)
Browse files Browse the repository at this point in the history
Co-authored-by: Travis Patterson <travis.patterson@grafana.com>
  • Loading branch information
yincongcyincong and MasslessParticle authored Aug 1, 2024
1 parent e81345e commit 6dd6b65
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/logql/syntax/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,22 @@ func (m MultiStageExpr) reorderStages() []StageExpr {
func combineFilters(in []*LineFilterExpr) StageExpr {
result := in[len(in)-1]
for i := len(in) - 2; i >= 0; i-- {
leafNode(result).Left = in[i]
leaf := leafNode(result, in[i])
if leaf != nil {
leaf.Left = in[i]
}
}

return result
}

func leafNode(in *LineFilterExpr) *LineFilterExpr {
func leafNode(in *LineFilterExpr, child *LineFilterExpr) *LineFilterExpr {
current := in
//nolint:revive
for ; current.Left != nil; current = current.Left {
if current == child || current.Left == child {
return nil
}
}
return current
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/logql/syntax/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,3 +1093,24 @@ func TestGroupingString(t *testing.T) {
}
require.Equal(t, " without ()", g.String())
}

func TestCombineFilters(t *testing.T) {
in := []*LineFilterExpr{
{LineFilter: LineFilter{Ty: log.LineMatchEqual, Match: "test1"}},
{LineFilter: LineFilter{Ty: log.LineMatchEqual, Match: "test2"}},
}

var combineFilter StageExpr
for i := 0; i < 2; i++ {
combineFilter = combineFilters(in)
}

current := combineFilter.(*LineFilterExpr)
i := 0
for ; current.Left != nil; current = current.Left {
i++
if i > 2 {
t.Fatalf("left num isn't a correct number")
}
}
}

0 comments on commit 6dd6b65

Please sign in to comment.