Skip to content

Commit 0fede82

Browse files
authored
[tempfix] - add bounds check check to avoid panics (#3867)
<!-- Please create an issue to collect feedback prior to feature additions. Please also reference that issue in any PRs. If possible try to keep PRs scoped to one feature, and add tests for new features. --> ### Description: Add a defensive bounds check to prevent invalid span ranges in the Aho-Corasick pattern matching implementation. This change ensures that the start index of a match span never exceeds its end index, preventing potential runtime panics. Note: This is a temporary fix until the root cause is identified. ### Checklist: * [ ] Tests passing (`make test-community`)? * [ ] Lint passing (`make lint` this requires [golangci-lint](https://golangci-lint.run/welcome/install/#local-installation))?
1 parent 8cd2fdd commit 0fede82

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

pkg/engine/ahocorasick/ahocorasickcore.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ func (m *adjustableSpanCalculator) calculateSpan(params spanCalculationParams) m
9999
startIdx := max(startOffset, 0)
100100
endIdx := min(maxSize, int64(len(params.chunkData)))
101101

102+
// Ensure the start index is not greater than the end index to prevent invalid spans.
103+
// In rare cases where the calculated start index exceeds the end index (possibly due to
104+
// detector-provided offsets), we reset the start index to 0 to maintain a valid span range
105+
// and avoid runtime panics. This is a temporary fix until the root cause is identified.
106+
if startIdx >= endIdx {
107+
startIdx = 0
108+
}
109+
102110
return matchSpan{startOffset: startIdx, endOffset: endIdx}
103111
}
104112

0 commit comments

Comments
 (0)