From 3dd9ea52d8ecd11feabc81f21c88f15bb6db3344 Mon Sep 17 00:00:00 2001 From: Dreamacro <305009791@qq.com> Date: Sat, 14 Sep 2019 21:42:40 +0800 Subject: [PATCH] Fix(domain-trie): crash when insert --- component/domain-trie/tire.go | 2 +- component/domain-trie/trie_test.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/component/domain-trie/tire.go b/component/domain-trie/tire.go index 26062fc2..f4e87ff5 100644 --- a/component/domain-trie/tire.go +++ b/component/domain-trie/tire.go @@ -22,7 +22,7 @@ type Trie struct { } func isValidDomain(domain string) bool { - return domain[0] != '.' && domain[len(domain)-1] != '.' + return domain != "" && domain[0] != '.' && domain[len(domain)-1] != '.' } // Insert adds a node to the trie. diff --git a/component/domain-trie/trie_test.go b/component/domain-trie/trie_test.go index 5303596b..228106cd 100644 --- a/component/domain-trie/trie_test.go +++ b/component/domain-trie/trie_test.go @@ -26,6 +26,10 @@ func TestTrie_Basic(t *testing.T) { if !node.Data.(net.IP).Equal(localIP) { t.Error("should equal 127.0.0.1") } + + if tree.Insert("", localIP) == nil { + t.Error("should return error") + } } func TestTrie_Wildcard(t *testing.T) {