From fce86aad332decf332a266b70ad9b39837c93739 Mon Sep 17 00:00:00 2001 From: mengskysama Date: Wed, 23 Aug 2023 01:21:27 +0800 Subject: [PATCH] fix(common): strmatcher match domain safety --- common/strmatcher/ac_automaton_matcher.go | 6 +++++- common/strmatcher/strmatcher_test.go | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/common/strmatcher/ac_automaton_matcher.go b/common/strmatcher/ac_automaton_matcher.go index ab7c09bd..24be9dac 100644 --- a/common/strmatcher/ac_automaton_matcher.go +++ b/common/strmatcher/ac_automaton_matcher.go @@ -225,7 +225,11 @@ func (ac *ACAutomaton) Match(s string) bool { // 2. the match string is through a fail edge. NOT FULL MATCH // 2.1 Through a fail edge, but there exists a valid node. SUBSTR for i := len(s) - 1; i >= 0; i-- { - idx := char2Index[s[i]] + chr := int(s[i]) + if chr >= len(char2Index) { + return false + } + idx := char2Index[chr] fullMatch = fullMatch && ac.trie[node][idx].edgeType node = ac.trie[node][idx].nextNode switch ac.exists[node].matchType { diff --git a/common/strmatcher/strmatcher_test.go b/common/strmatcher/strmatcher_test.go index 2e48c1b7..408ae628 100644 --- a/common/strmatcher/strmatcher_test.go +++ b/common/strmatcher/strmatcher_test.go @@ -217,6 +217,10 @@ func TestACAutomaton(t *testing.T) { pattern: "vvgoogle.com", res: true, }, + { + pattern: "½", + res: false, + }, } for _, test := range cases2Output { if m := ac.Match(test.pattern); m != test.res { @@ -224,7 +228,6 @@ func TestACAutomaton(t *testing.T) { } } } - { cases3Input := []struct { pattern string