Skip to content

Commit 6dd6b65

Browse files
fix: ast left cycular reference result in oom (#13501)
Co-authored-by: Travis Patterson <travis.patterson@grafana.com>
1 parent e81345e commit 6dd6b65

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

‎pkg/logql/syntax/ast.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,22 @@ func (m MultiStageExpr) reorderStages() []StageExpr {
185185
func combineFilters(in []*LineFilterExpr) StageExpr {
186186
result := in[len(in)-1]
187187
for i := len(in) - 2; i >= 0; i-- {
188-
leafNode(result).Left = in[i]
188+
leaf := leafNode(result, in[i])
189+
if leaf != nil {
190+
leaf.Left = in[i]
191+
}
189192
}
190193

191194
return result
192195
}
193196

194-
func leafNode(in *LineFilterExpr) *LineFilterExpr {
197+
func leafNode(in *LineFilterExpr, child *LineFilterExpr) *LineFilterExpr {
195198
current := in
196199
//nolint:revive
197200
for ; current.Left != nil; current = current.Left {
201+
if current == child || current.Left == child {
202+
return nil
203+
}
198204
}
199205
return current
200206
}

‎pkg/logql/syntax/ast_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,3 +1093,24 @@ func TestGroupingString(t *testing.T) {
10931093
}
10941094
require.Equal(t, " without ()", g.String())
10951095
}
1096+
1097+
func TestCombineFilters(t *testing.T) {
1098+
in := []*LineFilterExpr{
1099+
{LineFilter: LineFilter{Ty: log.LineMatchEqual, Match: "test1"}},
1100+
{LineFilter: LineFilter{Ty: log.LineMatchEqual, Match: "test2"}},
1101+
}
1102+
1103+
var combineFilter StageExpr
1104+
for i := 0; i < 2; i++ {
1105+
combineFilter = combineFilters(in)
1106+
}
1107+
1108+
current := combineFilter.(*LineFilterExpr)
1109+
i := 0
1110+
for ; current.Left != nil; current = current.Left {
1111+
i++
1112+
if i > 2 {
1113+
t.Fatalf("left num isn't a correct number")
1114+
}
1115+
}
1116+
}

0 commit comments

Comments
 (0)